HTML5 Canvas实现图片缩放、翻转、颜色渐变的代码示例
程序员文章站
2023-08-17 21:22:28
这篇文章主要介绍了HTML5 Canvas实现图片缩放、翻转、颜色渐变的代码示例,充分利用到了坐标的操作,说明都写在代码注释里了很简明,需要的朋友可以参考下... 16-02-28...
翻转、移动、平移、放大、缩小
xml/html code复制内容到剪贴板
- var 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';
- 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);
- }
- // 平移
- 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);
- 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);
- }
渐变、图像组合效果、颜色翻转
xml/html code复制内容到剪贴板
- var canvas = document.getelementbyid('canvas');
- if (canvas.getcontext) {
- var context = canvas.getcontext('2d');
- // 线性绘制渐变
- var grd = context.createlineargradient(0,0,200,100);
- // postion 必须是0.1-1.0之间的竖直,表示渐变中颜色的地点相对地位,color表示颜色
- grd.addcolorstop(0.1, "#00ff00");
- grd.addcolorstop(0.8, "#ff0000");
- context.fillstyle = grd;
- context.fillrect(0,0, 200,100);
- // 径向渐变
- var grd = context.createradialgradient(100,100,10,100,100,50);
- grd.addcolorstop(0.1, "#00ff00");
- grd.addcolorstop(0.8, '#ff0000');
- context.fillstyle = grd;
- context.fillrect(0,0,200,200);
- // 图像组合效果
- context.fillstyle = '#00ff00';
- context.fillrect(10,10,50,50);
- // 新绘图
- //context.globalcompositeoperation = "source-over";
- // 只绘制新内容,删除其他所有内容
- context.globalcompositeoperation = 'copy';
- // 图形重叠的地方,其颜色值相减后决定
- context.globalcompositeoperation = 'darker';
- // 画布上已经有的内容只会载和其他图形重叠的地方保留
- context.globalcompositeoperation = 'destination-atop';
- // 参考 http://www.w3school.com.cn/htmldom/prop_canvasrenderingcontext2d_globalcompositeoperation.asp
- context.beginpath();
- context.fillstyle = '#ff0000';
- context.arc(50,50,30,0, 2 * math.pi);
- context.fill();
- // 颜色翻转
- 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];
- pixels[i+2] = 255 - pixels[i + 2];
- }
- context.putimagedata(imgdata, 250, 0);
- }
- }
上一篇: 春季减肥,知道这些东西让你事半功倍哦!
下一篇: 卵巢囊肿吃什么好