简体   繁体   English

画布偏离中心,无法正常运行

[英]Canvas drawing off center, Erase doesn't work correctly

I wrote a JavaScript that allows a user to draw with their mouse on an HTML5 canvas (similar to MS Paint). 我写了一个JavaScript,允许用户用鼠标在HTML5画布(类似于MS Paint)上绘制。

Right now, I have 2 problems: 现在,我有两个问题:

  1. The drawing feature only works if the HTML5 canvas element is positioned at the top left corner (0, 0) of the web page, otherwise it doesn't work at all OR the drawing is off center. 仅当HTML5 canvas元素位于网页的左上角(0,0)时,绘图功能才起作用,否则它根本不起作用或绘图偏离中心。

  2. I'm unable to erase the drawing. 我无法删除图形。 When I erase the drawing it erases BUT as soon as I start drawing again, it comes back. 当我擦除图形时,它会在再次开始绘制时立即擦除BUT,然后又恢复。

My code is below: 我的代码如下:

HTML Canvas HTML画布

<canvas id="can1" width="500" height="500"></canvas>1

JavaScript for Canvas Drawing 画布绘图JavaScript

// Variables
var x1;
var y1;
var isPressed = false;
var myCanvas;
var myContext;

function startCanvas() {

    // Canvas stuff
    myCanvas = document.getElementById("can1");
    myContext = myCanvas.getContext("2d");

    // Specify a black background, and white lines that are 3 pixels thick.
    myContext.fillStyle = '#fff';
    myContext.strokeStyle = '#fff';
    myContext.fillRect(0, 0, 500, 500);
    myContext.lineWidth = 3;
    myContext.fill();
}

function functionMouseDown(e) {
    // Get coordinates
    x1 = e.clientX
    y1 = e.clientY;

    isPressed = true;
}

function functionMouseMove(e) {
    // If mouse is down and moved start drawing line
    if (isPressed == true) {
        drawLine(e);
    }
}

function functionMouseUp() {
    // Stop drawing line
    isPressed = false;
    //myContext.closePath();
    //myContext.stroke();
}

function drawLine(e) {
    // Draw line
    var x = e.clientX;
    var y = e.clientY;

    myContext.strokeStyle = '#cc0000';
    myContext.lineWidth = 1;
    myContext.moveTo(x1, y1);
    myContext.lineTo(x, y);
    myContext.stroke();

    // Set start coordinates to current coordinates
    x1 = x;
    y1 = y;
}

JavaScript that I use to erase canvas: 我用来擦除画布的JavaScript:

myContext.clearRect(0, 0, 500, 500);

I use the following function to accomplish this 我使用以下功能来完成此操作

function relMouseCoords(event){/*needs fixing for general case*/
    var totalOffsetX = 0
    var totalOffsetY = 0
    var canvasX = 0
    var canvasY = 0
    var currentElement = this

    do{
        totalOffsetX += currentElement.offsetLeft
        totalOffsetY += currentElement.offsetTop
    }
    while(currentElement = currentElement.offsetParent)

    canvasX = event.pageX - totalOffsetX
    canvasY = event.pageY - totalOffsetY

    return {x:canvasX, y:canvasY}
}

HTMLCanvasElement.prototype.relMouseCoords = relMouseCoords;

then 然后

var cord = e.target.relMouseCoords(e);
x1 = cord.x; 
y1 = cord.y;
...
var cord = e.target.relMouseCoords(e);
var x = cord.x;
var y =cord.y;

http://jsfiddle.net/mowglisanu/u3rvT/1/ http://jsfiddle.net/mowglisanu/u3rvT/1/

最简单的解决方案是使用myCanvas.offsetLeft和myCanvas.offsetTop设置画布的偏移量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM