/// <summary> /// Performs pixel level collision between two masks /// </summary> /// <param name="mask1">The mask1.</param> /// <param name="mask2">The mask2.</param> /// <param name="x">The x.</param> /// <param name="y">The y.</param> /// <returns></returns> public static bool Collision(string mask1, string mask2, float x, float y) { CanvasElement canvas1 = _masks[mask1]; if (x > canvas1.Width || y > canvas1.Height) { return(false); } CanvasElement canvas2 = _masks[mask2]; if (canvas2.Width + x < 0 || canvas2.Height + y < 0) { return(false); } int top = Math.Round(Math.Max(0, y)); int height = Math.Round(Math.Min(canvas1.Height, y + canvas2.Height) - top); int left = Math.Round(Math.Max(0, x)); int width = Math.Round(Math.Min(canvas1.Width, x + canvas2.Width) - left); if (width <= 0 || height <= 0) { return(false); } CanvasElement checkCanvas = (CanvasElement)Document.CreateElement("Canvas"); checkCanvas.Width = width; checkCanvas.Height = height; CanvasContext2D context = (CanvasContext2D)checkCanvas.GetContext(Rendering.Render2D); context.FillStyle = "white"; context.FillRect(0, 0, checkCanvas.Width, checkCanvas.Height); context.CompositeOperation = CompositeOperation.Xor; context.DrawImage(canvas1, left, top, width, height, 0, 0, width, height); context.DrawImage(canvas2, Math.Round(left - x), Math.Round(top - y), width, height, 0, 0, width, height); PixelArray data = context.GetImageData(0, 0, width, height).Data; for (int i = 0; i < data.Length; i += 4) { if ((int)data[i] > 0) { return(true); } } return(false); }
public Color GetColorFromClick(ElementEvent e) { ImageElement image = Document.GetElementById <ImageElement>("colorhex"); CanvasElement canvas = (CanvasElement)Document.CreateElement("canvas"); canvas.Width = image.Width; canvas.Height = image.Height; CanvasContext2D ctx = (CanvasContext2D)canvas.GetContext(Rendering.Render2D); ctx.DrawImage(image, 0, 0); PixelArray pixels = ctx.GetImageData(e.OffsetX, e.OffsetY, 1, 1).Data; Color = Color.FromArgb((float)pixels[3], (float)pixels[0], (float)pixels[1], (float)pixels[2]); return(Color); }
public void PickColor(ElementEvent e) { DivElement picker = Document.GetElementById <DivElement>("colorpicker"); ImageElement image = Document.GetElementById <ImageElement>("colorhex"); CanvasElement canvas = (CanvasElement)Document.CreateElement("canvas"); canvas.Width = image.Width; canvas.Height = image.Height; CanvasContext2D ctx = (CanvasContext2D)canvas.GetContext(Rendering.Render2D); ctx.DrawImage(image, 0, 0); PixelArray pixels = ctx.GetImageData(e.OffsetX, e.OffsetY, 1, 1).Data; Color = Color.FromArgb((float)pixels[3], (float)pixels[0], (float)pixels[1], (float)pixels[2]); if (CallBack != null) { CallBack(Color); } }