使用javascript

时间:2015-06-18 06:57:47

标签: javascript canvas zoom jpeg mousewheel

我在java脚本中编写了简单的代码,它在画布上添加了JPEG图像。使用简单的mousedown& amp;在该图像上添加线条鼠标事件。现在我想添加放大和放大缩小对该图像的影响。任何人都可以帮助我吗?

我的js文件如下所示。

var canvas;
var context;
var width;
var height;
var finalPos = {x: 0, y: 0};
var startPos = {x: 0, y: 0};


function main() {

    canvas = document.getElementById('imageCanvas');
    context = canvas.getContext('2d');
    loadImage();

    width = canvas.width;
    height = canvas.height;

    var canvasOffset = $('#imageCanvas').offset();
    function line(cnvs) {
        cnvs.beginPath();
        cnvs.moveTo(startPos.x, startPos.y);
        cnvs.lineTo(finalPos.x, finalPos.y);
        cnvs.stroke();
    }

    $('#imageCanvas').mousedown(function (e) {
        context.strokeStyle = 'blue';
        context.lineWidth = 5;
        context.lineCap = 'round';
        context.beginPath();
        startPos = {x: e.pageX - canvasOffset.left, y: e.pageY - canvasOffset.top};
    });

    $('#imageCanvas').mouseup(function (e) {
        // Replace with var that is second canvas
        finalPos = {x: e.pageX - canvasOffset.left, y: e.pageY - canvasOffset.top};
        line(context);
    });
}
;

function loadImage() {
    system_image = new Image();
    system_image.src = 'img/Google_OH_406TwentySecondStreet.jpg';

    system_image.onload = function () {
        context.drawImage(system_image, 0, 0, width, height);
    };
}

1 个答案:

答案 0 :(得分:0)

您可以在鼠标滚轮上使用缩放功能

context.scale(scaleValue, scaleValue);

你必须在上下文的drawimage之前做到这一点。

我在这里创建了示例:[link]:http://jsfiddle.net/itsjawad/5wohz3r1/