html5 更新图片颜色示例代码
发布时间:2015-05-12 来源:查字典编辑
摘要:代码如下:varcID="c1";varimage=newImage();image.src="Eye/item_eye_1.png";im...
代码如下:
<canvas id="c1" width="1220" height = "880" style="background: none repeat scroll 0% 0% transparent; "></canvas>
<script>
var cID = "c1";
var image = new Image();
image.src = "Eye/item_eye_1.png";
image.onload = function () {
recolorImage(cID,image, 0, 0, 0, 255, 0, 0);
}
function recolorImage(c,img, oldRed, oldGreen, oldBlue, newRed, newGreen, newBlue) {
var c = document.getElementById(c);
var ctx = c.getContext("2d");
var w = img.width;
var h = img.height;
c.width = w;
c.height = h;
// draw the image on the temporary canvas
ctx.drawImage(img, 0, 0, w, h);
// pull the entire image into an array of pixel data
var imageData = ctx.getImageData(0, 0, w, h);
// examine every pixel,
// change any old rgb to the new-rgb
for (var i = 0; i < imageData.data.length; i += 4) {
// is this pixel the old rgb?
if (imageData.data[i] == oldRed && imageData.data[i + 1] == oldGreen && imageData.data[i + 2] == oldBlue) {
// change to your new rgb
imageData.data[i] = newRed;
imageData.data[i + 1] = newGreen;
imageData.data[i + 2] = newBlue;
}
}
// put the altered data back on the canvas
ctx.putImageData(imageData, 0, 0);
}
</script>
相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
- ■Win10 TH2正式版升级遇到错误0xC1900101的临时解决办法
- ■win10 th2正式版10586十一月更新出错 提示"参数不正确"
- ■2015年win10共发布135个安全补丁 创历年之最
- ■win10/win7/win8.1十二月首个安全更新发布 修复14项重要bug
- ■三种助你开关Win10中飞行模式的方法
- ■Win10 TH2正式版引入更安全的新BitLocker加密算法
- ■怎么查看Win10下WinSxS文件夹实际大小? 一个命令搞定
- ■双系统怎么安装Win10 TH2正式版? Win7、Win8.1双系统图文安装教程
- ■轻轻松松搞定Win10的激活问题
- ■微软官方版屏蔽Win10升级通知图标的技巧
- ■十种办法解决安装Win10 TH2正式版卡住不动的问题
- ■Win10 TH2首个重要更新后应用商店依然存在问题
- ■简单四步帮你解决Win10常见的激活问题
- ■win8.1开机出错pniopcac.exe报错该怎么办?
- ■Win10 Mobile 准正式版10586.11修复内容和已知问题汇总
- ■诺基亚1320安装Win10 Mobile 10586.29更新了什么内容?
- ■win10 th2正式版重新上线 安装卡在44%的问题依然没有解决
- ■Win10 Mobile Redstone版本号确定为Build 11082明年发布
- ■win10打开或关闭飞行模式的方法
- ■手机更新升级Win10 Mobile正式版要求及注意事项汇总
- ■Win10 Mobile触摸应用更新 设置页面进化
- ■Win10 th2正式版首个重大更新普遍性Bug 开机速度超慢
- ■Win7/Win8.1/Win10的UAC对话框“是”点不了的原因及解决方法
- ■WIN10去除、恢复6个文件夹(视频、图片、文档、下载、音乐、桌面)的方法
- ■Win10正式版怎么关闭windows deender 关闭windows deender教程
- ■微软警告 重置过的Win10 Mobile 10581切勿直接升级10586
- ■Win10 th2正式版更新累计补丁KB3116908安装失败怎么办?
- ■微软撤回Win10 TH2正式版(10586)官方镜像ISO下载
- ■Win10首个重大更新竟然自动卸载多款第三方杀毒软件
- ■Win10 Mobile 预览版10586.11现身 升级界面曝光
最新HTML5教程学习
网页设计子分类