Javascript 图像处理 反转颜色
我正在尝试使用 javascript 处理网页内的图像,这很痛苦。 除了浏览器之外,我没有编码平台 atm。我认为它应该反转颜色,但我不确定是什么搞砸了。我的脚本有什么问题吗?
<img src="C:\Documents and Settings\_username_\Desktop\rabbit.jpg" name="myimage" id="myimage">
<input type="button" value="Click Here" onClick="doSomething();">
<SCRIPT language=JavaScript>
function doSomething(){
var imgd = myimage.getImageData(x, y, width, height);
var pix = imgd.data;
for (var i = 0, n = pix.length; i < n; i += 4) {
pix[i ] = 255 - pix[i ]; // red
pix[i+1] = 255 - pix[i+1]; // green
pix[i+2] = 255 - pix[i+2]; // blue
// i+3 is alpha (the fourth element)
}
myimage.putImageData(imgd, x, y);
}
</SCRIPT>
I am trying to play with an image inside a webpage using javascript and it is being a pain.
I have no coding platform atm except my browser. It should invert the color I think but i'm not sure what is screwing up. What is wrong with my script?
<img src="C:\Documents and Settings\_username_\Desktop\rabbit.jpg" name="myimage" id="myimage">
<input type="button" value="Click Here" onClick="doSomething();">
<SCRIPT language=JavaScript>
function doSomething(){
var imgd = myimage.getImageData(x, y, width, height);
var pix = imgd.data;
for (var i = 0, n = pix.length; i < n; i += 4) {
pix[i ] = 255 - pix[i ]; // red
pix[i+1] = 255 - pix[i+1]; // green
pix[i+2] = 255 - pix[i+2]; // blue
// i+3 is alpha (the fourth element)
}
myimage.putImageData(imgd, x, y);
}
</SCRIPT>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用的是 MS Internet Explorer,则以下代码应该适用于您
在 IE 8 之后,“filter”应替换为“-ms-filter”
也是一个更智能的方法sohtanaka网站提到的方式,使用jquery和css来完成所需的效果
http://www.sohtanaka.com/web- design/grayscale-hover-effect-w-css-jquery/
希望这对您有帮助:-)
If you are using MS Internet Explorer, the following code should work for you
Post IE 8 it the "filter" should be replaced by "-ms-filter"
Also a smarter way is mentioned at the sohtanaka site, which uses jquery and css to accomplish the required effect
http://www.sohtanaka.com/web-design/greyscale-hover-effect-w-css-jquery/
Hope this helps you :-)
xolor 库 有一个
inverse
函数:它的实现是这样的,其中它已经解析了作为 xolor 对象属性存在的红色、绿色和蓝色值:
The xolor library has an
inverse
function:Its implemented like this, where it already parsed the red, green, and blue values which exist as properties on the xolor object:
这适用于
canvas
上下文,但您正在处理图像。创建一个画布
并将图像数据复制到其中,然后反转颜色。That would work on a
canvas
context, but you're working with an image. Create acanvas
and copy the image's data into it and then invert the colors.