博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
js -- canvas img 封装
阅读量:6578 次
发布时间:2019-06-24

本文共 4911 字,大约阅读时间需要 16 分钟。

鼠标   1.操作canvas 中的 img。 右键放大缩小,左键移动img。 

   2.拖动input type= range  改变图片的透明度

html 代码 

    
图片已中心店的坐标缩放

js 代码

/** * Created by Administrator on 2018/7/6. */function doNothing() {    window.event.returnValue = false;    return false;}var imgX = 0;var imgY = 0;var imgScale = 1;var val = 0;window.onload = function (event) {    var count = [        {cas2: 'cas1', box2: '#box1,#box2', path: './2.jpg', callback: imgOnload_callbak, scale: methods,icon_img:'./4.jpg,./5.jpg',inp_id:'#inp_d,#inp_d'},        {cas2: 'cas2', box2: '#box1,#box2,',path: './3.jpg', callback: imgOnload_callbak, scale: methods,icon_img:'./5.jpg,./4.jpg',inp_id:'#inp_d,#inp_d'}    ];    for (var i = 0; i < count.length; i++) {        getElement(count[i].cas2, count[i].box2, count[i].path, count[i].callback, count[i].scale,count[i].icon_img,count[i].inp_id)    }};function getElement(cas_id, box_id, img_src, imgOnload_call, methods,icon_img,inp_id) {    var canvas = document.getElementById(cas_id);    var box = document.getElementById(box_id);    var arr = box_id.split(',');    var context = canvas.getContext('2d');    var img = new Image();    var img1 = new Image();    var img1_arr = icon_img.split(',');    img.src = img_src;    for(var i = 0; i < img1_arr.length;i++){        img1.src = img1_arr[i];    }    imgOnload_call(img, context, canvas, img1);    methods(arr, canvas, context, img,img1);    inp_methods(inp_id,context,canvas,img,img1)}function imgOnload_callbak(img,context,canvas,img1) {    img.onload = function () {        context.globalAlpha = 1.0;        context.drawImage(img, 0, 0);    };    img1.onload = function (){        setTimeout(function(){            context.globalAlpha = 0;            context.drawImage(img1,0,0,img1.width,img1.height,0,0,img1.width,img1.height);        },50);    }}function methods(elements, canvas, context, img,img1) {    var img_y = 1;    elements.forEach(function (item, i) {        $(item).mousedown(function (event) {            console.log(val);            var e_btn = event.button;            if (e_btn == 2) {                var e_down_y = event.clientY;                $(item).mousemove(function (event) {                    var e_move_y = event.clientY;                    if (e_move_y < e_down_y) {                        img_y *= 1.01;                    } else {                        img_y /= 1.01;                    }                    e_down_y = e_move_y;                    img_y = (img_y > 1) ? img_y : 1;                    canvas.style.transform = "scale(" + img_y + "," + img_y + ")"                });                $(item).mouseup(function () {                    $(item).unbind('mousemove');                    $(item).unbind('mouseup');                })            }            else if (e_btn == 0) {                var e_drag_down = {                    x: event.clientX,                    y: event.clientY                };                $(item).mousemove(function (event) {                    var e_drag_move = {                        x: event.clientX,                        y: event.clientY                    };                    var e_drag_distance = {                        x: e_drag_move.x - e_drag_down.x,                        y: e_drag_move.y - e_drag_down.y                    };                    e_drag_down = e_drag_move;                    imgX += e_drag_distance.x;                    imgY += e_drag_distance.y;                    context.clearRect(0, 0, canvas.width, canvas.height);                    context.globalAlpha = 1.0;                    context.drawImage(img, 0, 0, img.width, img.height, (imgX / 5), (imgY / 5), img.width * imgScale, img.height * imgScale);                    context.globalAlpha = val;                    context.drawImage(img1, 0, 0, img1.width, img1.height, (imgX / 5), (imgY / 5), img1.width * imgScale, img1.height * imgScale);                });                $(item).mouseup(function () {                    $(item).unbind("mousemove");                    $(item).unbind('mouseup');                })            }        })    })}function inp_methods(inp_id,context,canvas,img,img1){    var inp_id_arr = inp_id.split(',');    inp_id_arr.forEach(function(item,i){        $(item).on('input propertychange',function(){            val = $(item).val();            val = ( val < 0.1)? 0.1 : val/100;            context.clearRect(0,0,canvas.width,canvas.height);            context.globalAlpha = 1.0;            context.drawImage(img,0,0,img.width, img.height, (imgX / 5), (imgY / 5), img.width * imgScale, img.height * imgScale);            context.globalAlpha = val;            context.drawImage(img1,0,0,img1.width, img1.height, (imgX / 5), (imgY / 5), img1.width * imgScale, img1.height * imgScale);        })    });}

 有错误的地方,请大家帮忙 指正一下。谢谢!

转载于:https://www.cnblogs.com/yuerdong/p/9299727.html

你可能感兴趣的文章
解决 Ubuntu 16.04 LTSSublime text3中文问题
查看>>
mysql主从复制实现数据库同步
查看>>
日期类
查看>>
面试-1
查看>>
CentOS自动登录Gnome
查看>>
第一章,重点总结
查看>>
nmon 安装
查看>>
LeetCode - 49. Group Anagrams
查看>>
移动前端不得不了解的html5 head 头标签
查看>>
Tomcat 服务器性能优化
查看>>
【框架学习】ibatis DAO框架分析
查看>>
ZOJ 3640 Help Me Escape
查看>>
Eclipse开发工具介绍
查看>>
putty与emacs
查看>>
Windows上验证过的一些乱七八糟的笔记
查看>>
设置桌面背景
查看>>
C#下实现的半角转与全角的互转
查看>>
PreparedStatement vs Statement
查看>>
使用texturePaker批量转化pvr为pn
查看>>
自我介绍
查看>>