渐变HTML5 Canvas实现图片缩放、翻转、颜色渐变代码示例

渐变图片  时间:2021-04-21  阅读:()

★精品文档★

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

DMIT$10.9/月,日本VPS/三网直连/1核1.5G内存/20GB存储/1Gbps端口

优惠码年付一次性5折优惠码:TYO-Lite-Open-Beta-1y-50OFF永久8折优惠码:TYO-Lite-Open-Beta-Recur-20OFF日本vpsCPU内存SSD流量带宽价格购买1核1.5G20 GB4 TB1Gbps$10.9/月购买2核2 G40 GB6 TB1Gbps$16.9/月购买2核4 G60 GB8 TB1Gbps$21.9/月购买4核4 G80 GB12 TB...

统计一下racknerd正在卖的超便宜VPS,值得推荐的便宜美国VPS

racknerd从成立到现在发展是相当迅速,用最低的价格霸占了大部分低端便宜vps市场,虽然VPS价格便宜,但是VPS的质量和服务一点儿都不拉跨,服务器稳定、性能给力,尤其是售后方面时间短技术解决能力强,估计这也是racknerd这个品牌能如此成功的原因吧! 官方网站:https://www.racknerd.com 多种加密数字货币、信用卡、PayPal、支付宝、银联、webmoney,可...

数脉科技:阿里云香港CN2线路服务器;E3-1230v2/16G/240G SSD/10Mbps/3IP,月付374元

数脉科技怎么样?昨天看到数脉科技发布了7月优惠,如果你想购买香港服务器,可以看看他家的产品,性价比还是非常高的。数脉科技对香港自营机房的香港服务器进行超低价促销,可选择10M、30M的优质bgp网络。目前商家有优质BGP、CN2、阿里云线路,国内用户用来做站非常不错,目前E3/16GB阿里云CN2线路的套餐有一个立减400元的优惠,有需要的朋友可以看看。点击进入:数脉科技商家官方网站香港特价阿里云...

渐变图片为你推荐
党建搜狗浏览器2设置xp支持ipadiexplore.exe应用程序错误iexplore.exe应用程序错误icloudiphone自己用icloud把iPhone抹掉了.激活却不是自己的id怎么破win7关闭135端口windows 7如何关闭139端口firefoxflash插件Firefox浏览器怎么激活adobe flash插件morphvoxpro怎么用如何使用MorphVOX Pro变声morphvoxpro怎么用MorphVOX Pro变声器声音怎样调试android5.1android 5.1是什么意思
域名转让 优惠码 美国主机论坛 免备案空间 eq2 湖南服务器托管 七夕促销 免费个人空间 789电视 nerds 河南移动网 服务器硬件防火墙 闪讯官网 空间购买 网通服务器 工信部网站备案查询 阿里云邮箱登陆地址 腾讯网盘 服务器防御 锐速 更多