gpt4 book ai didi

javascript - 非透明像素的命中检测

转载 作者:可可西里 更新时间:2023-11-01 02:14:34 30 4
gpt4 key购买 nike

给定网络上下文中的 PNG 具有一些透明像素和一些非透明像素,Javascript 中是否有一种方法可以确定用户是否单击了非透明像素?仅 webkit 的解决方案是完全可以接受的。

最佳答案

1) 创建与图像大小相同的 HTML5 Canvas
2) 获取Canvas的上下文,drawImage(yourImage, 0, 0)
3) d = context.getImageData(0, 0, w of img, h of img)
4) d.data[(y*width+x)*4+3] 用于 alpha

canvas = document.createElement("canvas");  //Create HTML5 canvas: supported in latest firefox/chrome/safari/konquerer.  Support in IE9
canvas.width = img.width; //Set width of your rendertarget
canvas.height = img.height; // \ height \ \ \
ctx = canvas.getContext("2d"); //Get the 2d context [the thing you draw on]
ctx.drawImage(img, 0, 0); //Draw the picture on it.
id = ctx.getImageData(0,0, img.width, img.height); //Get the pixelData of image
//id.data[(y*width+x)*4+3] for the alpha value of pixel at x,y, 0->255

关于javascript - 非透明像素的命中检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2878511/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com