是否可以在画布中拖动图像以及在同一画布上绘制?

时间:2017-02-03 09:33:10

标签: javascript html5 canvas

在这里,我尝试在html5画布上绘图,以及上传一些图像并在画布中拖动它们。问题是我可以做一个或另一个但不能两者兼而有之。 我已经意识到画布必须在拖动之前清除,但是通过这样做我清除了绘图,如果我不清除它绘制的画布,但拖动图像会留下痕迹。任何人都可以指出我正确的方向。

        function init() {
        canvas = document.getElementById('can');
        ctx = canvas.getContext("2d");
        w = canvas.width;
        h = canvas.height;
        img = document.getElementById("drag");

        canvas.addEventListener("mousemove", function (e) { findxy('move', e) }, false);
        canvas.addEventListener("mousedown", function (e) { findxy('down', e) }, false);
        canvas.addEventListener("mouseup", function (e) {  findxy('up', e) }, false);
        canvas.addEventListener("mouseout", function (e) { findxy('out', e) }, false);

        imageLoader = document.getElementById('imageLoader');
        imageLoader.addEventListener('change', handleImage, false);

        var contexts = [];
        contexts.push(canvas.getContext('2d'));

        function clearAll() {
            ctx.clearRect(0, 0, canvas.width, canvas.height);
        }

       canvas.onclick = function (e) { handleClick(e, 1); };

        function handleClick(e, contextIndex) {
            e.stopPropagation();
            var mouseX = parseInt(e.clientX - e.target.offsetLeft);
            var mouseY = parseInt(e.clientY - e.target.offsetTop);
        //    clearAll();
            for (var i = 0; i < states.length; i++) {

                var state = states[i];
                if (state.dragging) {
                    state.dragging = false;
                    state.draw();
                    continue;
                }

                if (state.contextIndex === contextIndex
                        && mouseX > state.x && mouseX < state.x + state.width
                        && mouseY > state.y && mouseY < state.y + state.height)
                {
                    state.dragging = true;
                    state.offsetX = mouseX - state.x;
                    state.offsetY = mouseY - state.y;
                    state.contextIndex = contextIndex;
                }
                state.draw();
            }
        }
        canvas.onmousemove = function (e) { handleMousemove(e, 1); }

        function handleMousemove(e, contextIndex) {
            e.stopPropagation();

            var mouseX = parseInt(e.clientX - e.target.offsetLeft);
            var mouseY = parseInt(e.clientY - e.target.offsetTop);

        //    clearAll();

            for (var i = 0; i < states.length; i++) {

                var state = states[i];

                if (state.dragging) {
                    state.x = mouseX - state.offsetX;
                    state.y = mouseY - state.offsetY;
                    state.contextIndex = contextIndex;
                }
                state.draw();
            }
        }
       var states = [];
        states.push(addState(0, 0, img));

        function addState(x, y, image) {
            state = {}
            state.dragging = false;
            state.contextIndex = 1;
            state.image = image;
            state.x = x;
            state.y = y;
            state.width = image.width;
            state.height = image.height;
            state.offsetX = 0;
            state.offsetY = 0;
            state.draw = function () {
                var context = contexts[this.contextIndex - 1];
                if (this.dragging) {
                    context.strokeStyle = 'red';
                    context.strokeRect(this.x, this.y, this.width + 5, this.height + 5);
                }
                context.drawImage(this.image, this.x, this.y);
            };
            state.draw();
            return(state);
        }

}//end of init()  

var imgArray = [];
function handleImage(e) {
    var reader = new FileReader();
    reader.onload = function (event) {
        imgArray.push(img);
        for(i = 0; i < imgArray.length; i++){
            img.src = imgArray[i];
            img.setAtX = i * 50;
            img.setAtY = i * 0;
            img.onload = function() {
                ctx.drawImage(this, this.setAtX, this.setAtY);
            };
            img.src = event.target.result;
        }
    };
    reader.readAsDataURL(e.target.files[0]);                
}
    function findxy(res, e) {
        if (res === 'down') {
            prevX = currX;
            prevY = currY;
            currX = e.clientX - canvas.offsetLeft;
            currY = e.clientY - canvas.offsetTop;

            flag = true;
            dot_flag = true;
            if (dot_flag) {
                ctx.beginPath();
                ctx.fillStyle = x;
                ctx.fillRect(currX, currY, 2, 2);
                ctx.closePath();
                dot_flag = false;
            }
        }
        if (res === 'up' || res === "out") {
            flag = false;
        }
        if (res === 'move') {
            if (flag) {
                prevX = currX;
                prevY = currY;
                currX = e.clientX - canvas.offsetLeft;
                currY = e.clientY - canvas.offsetTop;
                draw();
            }
        }
    }

    //Draw lines or text 
    function draw() {
        ctx.beginPath();
        ctx.moveTo(prevX, prevY);
        ctx.lineTo(currX, currY);
        ctx.strokeStyle = x;
        ctx.lineWidth = y;
        ctx.stroke();
        ctx.closePath();           
        ctx.fillStyle = x;           
        ctx.font = "Italic Bold 14pt Times, serif";            
        ctx.fillText(message, prevX, prevY);
    }

2 个答案:

答案 0 :(得分:0)

这是我链接到的代码和您自己的代码的混搭。

所有与绘制图像和在画布上进行平移相关的参数都包含在内。

&#13;
&#13;
var canvas = document.getElementById("canvas");
var ctx;
var x = 75;
var y = 50;
var WIDTH = 400;
var HEIGHT = 300;
var dragok = false;
var img = document.getElementById("img");
var lines = [{
  x: x,
  y: y
}];

function rect(x, y, w, h) {
  ctx.beginPath();
  ctx.rect(x, y, w, h);
  ctx.closePath();
  ctx.fill();
}

function clear() {
  ctx.clearRect(0, 0, WIDTH, HEIGHT);
}

function init() {
  canvas = document.getElementById("canvas");
  ctx = canvas.getContext("2d");
  return setInterval(draw, 1000/24);
}

function draw() {
  clear();
  ctx.drawImage(img, x - img.width / 2, y - img.height / 2);
  ctx.beginPath();
  ctx.moveTo(lines[0].x,lines[0].y);
  for(var i = 1; i < lines.length; i++) {
    var line = lines[i];
    ctx.lineTo(lines[i].x,lines[i].y);
  }
  ctx.stroke(); 
  ctx.closePath();
  //rect(x - 15, y - 15, 30, 30);
}

function myMove(e) {
  if (dragok) {
    x = e.pageX - canvas.offsetLeft;
    y = e.pageY - canvas.offsetTop;
    lines = lines.slice(lines.length - 100)
    lines.push({
      x: x,
      y: y
    });
  }
}

function myDown(e) {
  if (e.pageX < x + img.width / 2 + canvas.offsetLeft && e.pageX > x - img.width / 2 +
    canvas.offsetLeft && e.pageY < y + img.height / 2 + canvas.offsetTop &&
    e.pageY > y - img.height / 2 + canvas.offsetTop) {
    x = e.pageX - canvas.offsetLeft;
    y = e.pageY - canvas.offsetTop;
    dragok = true;
    canvas.onmousemove = myMove;
  }
}

function myUp() {
  dragok = false;
  canvas.onmousemove = null;
}

init();

canvas.onmousedown = myDown;
canvas.onmouseup = myUp;

//Change image for the heck of it ^^

setInterval(function() {
  img.src = 'https://placeholdit.imgix.net/~text?txtsize=60&txt=' +
    Math.floor(Math.random() * 10) +
    '&w=100&h=100';
}, 3000)
&#13;
<canvas id="canvas" width="400" height="300">
  This text is displayed if your browser does not support HTML5 Canvas.
</canvas>
<img id="img" src="https://placeholdit.imgix.net/~text?txtsize=60&txt=1&w=100&h=100">
&#13;
&#13;
&#13;

更新了我绘制线条的答案

答案 1 :(得分:0)

我的最终代码正在工作,我不得不为绘图添加两个画布,为拖动图像添加一个画布。织物框架的工作帮了很大忙。 https://jsfiddle.net/tn07Bond/982s4bgv/2/

<div id="canvasesdiv">
        <canvas id="images" width=600 height=400 style="position: absolute;
            left: 0;
            top: 0;
            border: 1px solid blue;
            z-index: 1;">
            This text is displayed if your browser does not support HTML5 Canvas</canvas>
        <canvas id="drawing" width=600 height=400 style="position: absolute;
            left: 0;
            top: 0;
            border: 1px solid red;
            z-index: 2;">
            This text is displayed if your browser does not support HTML5 Canvas</canvas>

        <input type="file" id="imageLoader" class="imageLoader" name="imageLoader"/> 

        <input type="image" src="images/PenForCanvas.png" alt="pen" title="Draw on canvas" 
               id="Drawing" style="cursor: pointer; margin: 5px 0 0 200px;">
        &nbsp;&nbsp;&nbsp;&nbsp;
        <input type="image" src="images/drag.png" alt="drag" title="Drag this image"  id="Images" style="cursor: pointer;">
    </div>


    <script>

        var drawing, drawingCtx,images,imagesCtx, message = "", img, imageLoader, prevX = 10, currX = 10, prevY = 10, currY = 10,
                dot_flag = false, flag = false, x = "black", y = 2, formElement, canvasLeft, canvasTop, imgData, data,
                startOffsetX = 0, startOffsetY = 0;

(function () {

        // Drawing canvas
        drawing = document.getElementById("drawing");
        drawingCtx = drawing.getContext("2d");

        //Uploading and dragging images
        images = new fabric.Canvas('images');
        document.getElementById('imageLoader').addEventListener("change", function (e) {
          var file = e.target.files[0];
          var reader = new FileReader();
          reader.onload = function (f) {
            var data = f.target.result;                    
            fabric.Image.fromURL(data, function (img) {
              var oImg = img.set({left: 0, top: 0, angle: 00,width:40, height:40}).scale(0.9);
              images.add(oImg).renderAll();
              var a = images.setActiveObject(oImg);
              var dataURL = images.toDataURL({format: 'png', quality: 0.8});
            });
          };
          reader.readAsDataURL(file);
        });

            w = drawing.width;
            h = drawing.height;

            //Mouse events
            drawing.addEventListener("mousemove", function (e) { findxy('move', e) }, false);
            drawing.addEventListener("mousedown", function (e) { findxy('down', e) }, false);
            drawing.addEventListener("mouseup", function (e) { findxy('up', e) }, false);
            drawing.addEventListener("mouseout", function (e) { findxy('out', e) }, false);

            Drawing = document.getElementById("Drawing");
            Drawing.addEventListener("click", bringDrawingToFront);
            Images = document.getElementById("Images");
            Images.addEventListener("click", bringImagesToFront);
            imageLoader = document.getElementById('imageLoader');
            imageLoader.addEventListener('change', bringImagesToFront);

        //Drawing
        function findxy(res, e) {
            if (res === 'down') {
                prevX = currX;
                prevY = currY;
                currX = e.clientX - drawing.offsetLeft;
                currY = e.clientY - drawing.offsetTop;

                flag = true;
                dot_flag = true;
                if (dot_flag) {
                    drawingCtx.beginPath();
                    drawingCtx.fillStyle = x;
                    drawingCtx.fillRect(currX, currY, 2, 2);
                    drawingCtx.closePath();
                    dot_flag = false;
                }
            }
            if (res === 'up' || res === "out") {
                flag = false;
            }
            if (res === 'move') {
                if (flag) {
                    prevX = currX;
                    prevY = currY;
                    currX = e.clientX - drawing.offsetLeft;
                    currY = e.clientY - drawing.offsetTop;
                    draw();
                }
            }
        }

        function save() {
            imgData = drawingCtx.getImageData(0, 0, drawing.width, drawing.height);
        }

        function restore() {
            drawingCtx.putImageData(imgData, 0, 0);
        }
        function bringDrawingToFront() {
            drawing.style.zIndex = 1;
            document.getElementById("images").style.zIndex = 0;
        }
        function bringImagesToFront() {
            drawing.style.zIndex = 0;
            document.getElementById("images").style.zIndex = 1;
        }

        //Draw lines or text 
        function draw() {
            drawingCtx.beginPath();
            drawingCtx.moveTo(prevX, prevY);
            drawingCtx.lineTo(currX, currY);
            drawingCtx.strokeStyle = x;
            drawingCtx.lineWidth = y;
            drawingCtx.stroke();
            drawingCtx.closePath();
            drawingCtx.fillStyle = x;
            drawingCtx.font = "Italic Bold 14pt Times, serif";
            drawingCtx.fillText(message, prevX, prevY);
            save();
        }

})();
    </script>