★精品文档★
HTML5 Canvas实现图片缩放、翻转、颜色
渐变代码示例
这篇文章主要介绍了 HTML5 Canvas实现图片缩放、翻转、颜色渐变的代码示例,充分利用到了坐标的操作,说明都写在代码注释里了很简明,需要的朋友可以参考下
翻转、移动、平移、放大、缩小
XML/HTML Codevar canvas = document.getElementById(‘canvas’); if (canvas.getContext) {
var context = canvas.getContext(‘2d’);
//放大与缩小
context.beginPath();
context.strokeStyle = “#000000”;
context.strokeRect(10,10,150,100);
//放大3倍
context.scale(3,3);
context.beginPath();
context.strokeStyle = ‘#cccccc’ ;
1/9
★精品文档★
context.strokeRect(10,10,150,100)
//缩小
context.scale(0.5,0.5);
context.beginPath();
context.strokeStyle = ‘#cccccc’ ;
context.strokeRect(10,10,150,100)
//翻转
var img=new Image();
img.src=‘ images/1 .jpg’ ;
img.onload=function(){
context.drawImage(img,10,10);
context.scale(1 , -1);
context.drawImage(img,0, -500);
}
//平移
2/9
★精品文档★
context.beginPath();
context.strokeStyle = ‘#000000’ ;
context.strokeRect(10,101 ,150,100);
// x移动 50 y移动 100
context.translate(50,100);
context.beginPath();
context.strokeStyle = ‘#cccccc’ ;
context.strokeRect(10,10,150,100);
//旋转
context.beginPath();
context.strokeStyle = ‘#000000’ ;
context.strokeRect(200,50,100,50);
//默认旋转是根据0,0 中心使用translate可以按照自己的设置的中心旋转
context.translate(250,75);
3/9
★精品文档★
context.rotate(45 * Math.PI /180);
context.translate(-250, -75);
context.beginPath();
context.strokeStyle = ‘#cccccc’ ;
context.strokeRect(200,50,100,50);
// transform矩阵
context.beginPath();
context.strokeStyle = ‘#000000’ ;
context.strokeRect(10,10,150,100);
context.transform(3,0,0,3,0,0);
context.beginPath();
context.strokeStyle = ‘#cccccc’ ;
context.strokeRect(10,10,150,100);
4/9
★精品文档★
}
渐变、 图像组合效果、颜色翻转
XML/HTML Codevar canvas = document.getElementById(‘canvas’); if (canvas.getContext) {
var context = canvas.getContext(‘2d’);
//线性绘制渐变
var grd =context.createLinearGrad ient(0,0,200,100);
// postion必须是0.1-1 .0之间的竖直表示渐变中颜色的地点相对地位 color表示颜色
grd.addColorStop(0.1 , “#00ff00”);
grd.addColorStop(0.8, “#ff0000”);
context.fi l lStyle=grd;
context.fi l lRect(0,0, 200,100);
//径向渐变
5/9
★精品文档★
var grd =context.createRadialGradient(100,100,10,100,100,50);
grd.addColorStop(0.1 , “#00ff00”);
grd.addColorStop(0.8, ‘#ff0000’);
context.fi l lStyle=grd;
context.fi l lRect(0,0,200,200);
//图像组合效果
context.fi l lStyle = ‘#00ff00’ ;
context.fi l lRect(10,10,50,50);
//新绘图
//context.globalCompositeOperation =“source-over”;
//只绘制新内容删除其他所有内容
6/9
★精品文档★
context.globalCompositeOperation = ‘copy’ ;
//图形重叠的地方其颜色值相减后决定
context.globalCompositeOperation = ‘darker’ ;
//画布上已经有的内容只会载和其他图形重叠的地方保留
context.globalCompositeOperation =‘destination-atop’ ;
// 参 考http://www.w3school .com.cn/htmldom/prop_canvasrenderingcontext2d_globalcompositeoperation.asp
context.beginPath();
context.fi l lStyle = ‘#ff0000’ ;
context.arc(50,50,30,0, 2 *Math.PI);
context.fi l l();
7/9
★精品文档★
//颜色翻转
var img = new Image();
img.src = ‘ images/1 .jpg’ ;
img.onload =function(){
context.drawImage(img,0,0, 1 , 1);
var imgData =context.getImageData(0,0, 1 ,1);
var pixels =imgData.data;
console. log(pixels);
for(var i=0, n =pixels. length; i< n; i+=4) {
pixels[i]=255-pixels[i];
pixels[i+1]=255-pixels[i+1];
8/9
★精品文档★
pixels[i+2]=255-pixels[i+2];
}
context.putImageData(imgData, 250, 0);
}
}
9/9
近日华纳云商家正式上线了美国服务器产品,这次美国机房上线的产品包括美国云服务器、美国独立服务器、美国高防御服务器以及美国高防云服务器等产品,新产品上线华纳云推出了史上优惠力度最高的特价优惠活动,美国云服务器低至3折,1核心1G内存5Mbps带宽低至24元/月,20G ddos高防御服务器低至688元/月,年付周期再送2个月、两年送4个月、三年送6个月,终身续费同价,有需要的朋友可以关注一下。华纳云...
有一段时间没有分享Gcore(gcorelabs)的信息了,这是一家成立于2011年的国外主机商,总部位于卢森堡,主要提供VPS主机和独立服务器租用等,数据中心包括俄罗斯、美国、日本、韩国、新加坡、荷兰、中国(香港)等多个国家和地区的十几个机房,商家针对不同系列的产品分为不同管理系统,比如VPS(Hosting)、Cloud等都是独立的用户中心体系,部落分享的主要是商家的Hosting(Virtu...
提速啦 成立于2012年,作为互联网老兵我们一直为用户提供 稳定 高速 高质量的产品。成立至今一直深受用户的喜爱 荣获 “2021年赣州安全大赛第三名” “2020创新企业入围奖” 等殊荣。目前我司在美国拥有4.6万G总内存云服务器资源,香港拥有2.2万G总内存云服务器资源,阿里云香港机房拥有8000G总内存云服务器资源,国内多地区拥有1.6万G总内存云服务器资源,绝非1 2台宿主机的小商家可比。...