前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >js -- canvas img 封装

js -- canvas img 封装

作者头像
小蔚
发布2019-09-11 14:28:43
6.4K0
发布2019-09-11 14:28:43
举报
文章被收录于专栏:小蔚记录小蔚记录

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

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

html 代码

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en" oncontextmenu="doNothing()">
<head>
    <meta charset="UTF-8">
    <title>图片已中心店的坐标缩放</title>
    <style>
        #box1 {
            width: 500px;
            height: 300px;
            border: 1px solid black;
            overflow: hidden;
            float: left;
        }

        #box2 {
            width: 500px;
            height: 300px;
            border: 1px solid black;
            overflow: hidden;
            float: left;
            margin-left: 50px;
            box-sizing: border-box;
        }
    </style>
    <script type="text/javascript">function ss() {
        if (event.button == 2) {
            alert("aaaa");
        }
        document.onmousedown = ss;
    }</script>
</head>
<body>
<div>
    <div id="box1">
        <canvas id="cas1" width="500" height="300"></canvas>
    </div>
    <div id='box2'>
        <canvas id="cas2" width="500" height="300"></canvas>
    </div>
    <div style="clear: both"></div>
</div>
<input type="range" id="inp_d" value="0">
</body>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="./index.js"></script>
</html>

js 代码

代码语言:javascript
复制
/**
 * 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);
        })
    });
}

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-07-12 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档