使用鼠标在 HTML5 Canvas 上绘图

Posted

技术标签:

【中文标题】使用鼠标在 HTML5 Canvas 上绘图【英文标题】:Draw on HTML5 Canvas using a mouse 【发布时间】:2011-01-23 01:13:26 【问题描述】:

我想用鼠标在 html Canvas 上绘图(例如,画签名、画名字……)

我将如何实施?

【问题讨论】:

检查这个! ***.com/questions/3814442/… 相关javascript library for free form drawing 另见:How do I hand draw on canvas? 好问题。谢谢 【参考方案1】:

这是一个工作示例。

 <html>
    <script type="text/javascript">
    var canvas, ctx, flag = false,
        prevX = 0,
        currX = 0,
        prevY = 0,
        currY = 0,
        dot_flag = false;

    var x = "black",
        y = 2;
    
    function init() 
        canvas = document.getElementById('can');
        ctx = canvas.getContext("2d");
        w = canvas.width;
        h = canvas.height;
    
        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);
    
    
    function color(obj) 
        switch (obj.id) 
            case "green":
                x = "green";
                break;
            case "blue":
                x = "blue";
                break;
            case "red":
                x = "red";
                break;
            case "yellow":
                x = "yellow";
                break;
            case "orange":
                x = "orange";
                break;
            case "black":
                x = "black";
                break;
            case "white":
                x = "white";
                break;
        
        if (x == "white") y = 14;
        else y = 2;
    
    
    
    function draw() 
        ctx.beginPath();
        ctx.moveTo(prevX, prevY);
        ctx.lineTo(currX, currY);
        ctx.strokeStyle = x;
        ctx.lineWidth = y;
        ctx.stroke();
        ctx.closePath();
    
    
    function erase() 
        var m = confirm("Want to clear");
        if (m) 
            ctx.clearRect(0, 0, w, h);
            document.getElementById("canvasimg").style.display = "none";
        
    
    
    function save() 
        document.getElementById("canvasimg").style.border = "2px solid";
        var dataURL = canvas.toDataURL();
        document.getElementById("canvasimg").src = dataURL;
        document.getElementById("canvasimg").style.display = "inline";
    
    
    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();
            
        
    
    </script>
    <body onload="init()">
        <canvas id="can"   style="position:absolute;top:10%;left:10%;border:2px solid;"></canvas>
        <div style="position:absolute;top:12%;left:43%;">Choose Color</div>
        <div style="position:absolute;top:15%;left:45%;width:10px;height:10px;background:green;" id="green" onclick="color(this)"></div>
        <div style="position:absolute;top:15%;left:46%;width:10px;height:10px;background:blue;" id="blue" onclick="color(this)"></div>
        <div style="position:absolute;top:15%;left:47%;width:10px;height:10px;background:red;" id="red" onclick="color(this)"></div>
        <div style="position:absolute;top:17%;left:45%;width:10px;height:10px;background:yellow;" id="yellow" onclick="color(this)"></div>
        <div style="position:absolute;top:17%;left:46%;width:10px;height:10px;background:orange;" id="orange" onclick="color(this)"></div>
        <div style="position:absolute;top:17%;left:47%;width:10px;height:10px;background:black;" id="black" onclick="color(this)"></div>
        <div style="position:absolute;top:20%;left:43%;">Eraser</div>
        <div style="position:absolute;top:22%;left:45%;width:15px;height:15px;background:white;border:2px solid;" id="white" onclick="color(this)"></div>
        <img id="canvasimg" style="position:absolute;top:10%;left:52%;" style="display:none;">
        <input type="button" value="save" id="btn" size="30" onclick="save()" style="position:absolute;top:55%;left:10%;">
        <input type="button" value="clear" id="clr" size="23" onclick="erase()" style="position:absolute;top:55%;left:15%;">
    </body>
    </html>

【讨论】:

为什么要切换?你也可以做 x = obj.id。完成。 我正在尝试使用此代码,但是当我没有在页面上一直向下滚动时,绘图会垂直关闭。我应该在此代码中更改什么? 我将canvas.offsetLeft;canvas.offsetTop; 分别更改为canvas.getBoundingClientRect().left;canvas.getBoundingClientRect().top; 以解决滚动问题。 这段代码让我走上了正确的道路。但我不得不说这段代码非常粗糙和丑陋。对于任何发现此问题并在鼠标位置上苦苦挣扎的人,请查看以下答案:***.com/a/17130415/5552144 要使其与绘图笔(如图形输入板)一起使用,必须将鼠标事件替换为触摸事件,例如。 touchmovetouchstarttouchend 然后clientX 来自e.touches["0"].clientX 代码中的findxy(),但还没有想到一种简单的方法来检测正在使用的内容,因为你听不到从我测试的结果来看,这两个事件同时发生。我按原样离开了mouseout。它并不完美,但确实有效【参考方案2】:

我想,这里的其他例子太复杂了。这个比较简单,只有JS...

// create canvas element and append it to document body
var canvas = document.createElement('canvas');
document.body.appendChild(canvas);

// some hotfixes... ( ≖_≖)
document.body.style.margin = 0;
canvas.style.position = 'fixed';

// get canvas 2D context and set him correct size
var ctx = canvas.getContext('2d');
resize();

// last known position
var pos =  x: 0, y: 0 ;

window.addEventListener('resize', resize);
document.addEventListener('mousemove', draw);
document.addEventListener('mousedown', setPosition);
document.addEventListener('mouseenter', setPosition);

// new position from mouse event
function setPosition(e) 
  pos.x = e.clientX;
  pos.y = e.clientY;


// resize canvas
function resize() 
  ctx.canvas.width = window.innerWidth;
  ctx.canvas.height = window.innerHeight;


function draw(e) 
  // mouse left button must be pressed
  if (e.buttons !== 1) return;

  ctx.beginPath(); // begin

  ctx.lineWidth = 5;
  ctx.lineCap = 'round';
  ctx.strokeStyle = '#c0392b';

  ctx.moveTo(pos.x, pos.y); // from
  setPosition(e);
  ctx.lineTo(pos.x, pos.y); // to

  ctx.stroke(); // draw it!

【讨论】:

@RyanCameron.Me 只需评论行if (e.buttons !== 1) return; ;-)。 @RyanCameron.Me 刚刚尝试了最新的 Chrome、Firefox 和 Edge,一切正常……你用什么浏览器? @RyanCameron.Me 这更有可能是因为我的resize 函数。我正在根据窗口大小设置画布的宽度和高度。你应该根据你的&lt;div class="container-fluid"&gt;来设置这些。 这有助于从设置位置函数的 y 坐标中减去大约 250。非常感谢您的帮助! @TaylorA.Leach 是的,如果canvas没有放在右上角,你需要在setPosition函数中添加一些offset...【参考方案3】:

这是使用画布创建绘图应用程序的最直接方法:

    mousedownmousemovemouseup 事件监听器附加到画布DOM 在mousedown 上,获取鼠标坐标,然后使用moveTo() 方法定位绘图光标并使用beginPath() 方法开始新的绘图路径。 在mousemove 上,用lineTo() 连续向路径添加一个新点,并用stroke() 为最后一段着色。 在mouseup 上,设置一个标志以禁用绘图。

从那里,您可以添加各种其他功能,例如让用户能够选择线条粗细、颜色、笔触甚至图层。

【讨论】:

如果有beginPath,mouseup后应该有closePath吗? @Timo 当你 stroke() 时,它会自动关闭路径。【参考方案4】:

用谷歌搜索了这个(“html5 画布绘制程序”)。看起来像你需要的。

http://dev.opera.com/articles/view/html5-canvas-painting/

【讨论】:

这个例子确实展示了如何画一个圆。不过其他的还是不错的。知道如何画圆吗?【参考方案5】:

检查此http://jsfiddle.net/ArtBIT/kneDX/。这应该会指引你朝着正确的方向前进

【讨论】:

完美。应该收到 chk。我需要一种在现有图像上制作简单填充矩形的方法。您的代码正是我需要的模板。 tyvm!【参考方案6】:

我也想使用这种方法进行签名,我在http://codetheory.in/ 上找到了一个示例。

我已将以下代码添加到 jsfiddle

HTML:

<div id="sketch">
    <canvas id="paint"></canvas>
</div>

Javascript:

 (function() 
    var canvas = document.querySelector('#paint');
    var ctx = canvas.getContext('2d');

    var sketch = document.querySelector('#sketch');
    var sketch_style = getComputedStyle(sketch);
    canvas.width = parseInt(sketch_style.getPropertyValue('width'));
    canvas.height = parseInt(sketch_style.getPropertyValue('height'));

    var mouse = x: 0, y: 0;
    var last_mouse = x: 0, y: 0;

    /* Mouse Capturing Work */
    canvas.addEventListener('mousemove', function(e) 
        last_mouse.x = mouse.x;
        last_mouse.y = mouse.y;

        mouse.x = e.pageX - this.offsetLeft;
        mouse.y = e.pageY - this.offsetTop;
    , false);


    /* Drawing on Paint App */
    ctx.lineWidth = 5;
    ctx.lineJoin = 'round';
    ctx.lineCap = 'round';
    ctx.strokeStyle = 'blue';

    canvas.addEventListener('mousedown', function(e) 
        canvas.addEventListener('mousemove', onPaint, false);
    , false);

    canvas.addEventListener('mouseup', function() 
        canvas.removeEventListener('mousemove', onPaint, false);
    , false);

    var onPaint = function() 
        ctx.beginPath();
        ctx.moveTo(last_mouse.x, last_mouse.y);
        ctx.lineTo(mouse.x, mouse.y);
        ctx.closePath();
        ctx.stroke();
    ;

());

【讨论】:

最好的部分是您可以通过右键单击不同的位置来绘制直线连接线。 :) 不适用于触摸屏。不是指平板电脑。我的意思是带有鼠标和触摸屏的 Windows 10 笔记本电脑。 我得到:未捕获的类型错误:无法在“窗口”上执行“getComputedStyle”:参数 1 不是“元素”类型。【参考方案7】:

这是我非常简单的画布绘制和擦除。

https://jsfiddle.net/richardcwc/d2gxjdva/

//Canvas
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
//Variables
var canvasx = $(canvas).offset().left;
var canvasy = $(canvas).offset().top;
var last_mousex = last_mousey = 0;
var mousex = mousey = 0;
var mousedown = false;
var tooltype = 'draw';

//Mousedown
$(canvas).on('mousedown', function(e) 
    last_mousex = mousex = parseInt(e.clientX-canvasx);
	last_mousey = mousey = parseInt(e.clientY-canvasy);
    mousedown = true;
);

//Mouseup
$(canvas).on('mouseup', function(e) 
    mousedown = false;
);

//Mousemove
$(canvas).on('mousemove', function(e) 
    mousex = parseInt(e.clientX-canvasx);
    mousey = parseInt(e.clientY-canvasy);
    if(mousedown) 
        ctx.beginPath();
        if(tooltype=='draw') 
            ctx.globalCompositeOperation = 'source-over';
            ctx.strokeStyle = 'black';
            ctx.lineWidth = 3;
         else 
            ctx.globalCompositeOperation = 'destination-out';
            ctx.lineWidth = 10;
        
        ctx.moveTo(last_mousex,last_mousey);
        ctx.lineTo(mousex,mousey);
        ctx.lineJoin = ctx.lineCap = 'round';
        ctx.stroke();
    
    last_mousex = mousex;
    last_mousey = mousey;
    //Output
    $('#output').html('current: '+mousex+', '+mousey+'<br/>last: '+last_mousex+', '+last_mousey+'<br/>mousedown: '+mousedown);
);

//Use draw|erase
use_tool = function(tool) 
    tooltype = tool; //update
canvas 
    cursor: crosshair;
    border: 1px solid #000000;
<canvas id="canvas"  ></canvas>
<input type="button" value="draw" onclick="use_tool('draw');" />
<input type="button" value="erase" onclick="use_tool('erase');" />
<div id="output"></div>

【讨论】:

【参考方案8】:

我必须为这个主题提供一个简单的例子,所以我会在这里分享:

http://jsfiddle.net/Haelle/v6tfp2e1

class SignTool 
  constructor() 
    this.initVars()
    this.initEvents()
  

  initVars() 
    this.canvas = $('#canvas')[0]
    this.ctx = this.canvas.getContext("2d")
    this.isMouseClicked = false
    this.isMouseInCanvas = false
    this.prevX = 0
    this.currX = 0
    this.prevY = 0
    this.currY = 0
  

  initEvents() 
    $('#canvas').on("mousemove", (e) => this.onMouseMove(e))
    $('#canvas').on("mousedown", (e) => this.onMouseDown(e))
    $('#canvas').on("mouseup", () => this.onMouseUp())
    $('#canvas').on("mouseout", () => this.onMouseOut())
    $('#canvas').on("mouseenter", (e) => this.onMouseEnter(e))
  
  
  onMouseDown(e) 
  	this.isMouseClicked = true
    this.updateCurrentPosition(e)
  
  
  onMouseUp() 
  	this.isMouseClicked = false
  
  
  onMouseEnter(e) 
  	this.isMouseInCanvas = true
    this.updateCurrentPosition(e)
  
  
  onMouseOut() 
  	this.isMouseInCanvas = false
  

  onMouseMove(e) 
    if (this.isMouseClicked && this.isMouseInCanvas) 
    	this.updateCurrentPosition(e)
      this.draw()
    
  
  
  updateCurrentPosition(e) 
      this.prevX = this.currX
      this.prevY = this.currY
      this.currX = e.clientX - this.canvas.offsetLeft
      this.currY = e.clientY - this.canvas.offsetTop
  
  
  draw() 
    this.ctx.beginPath()
    this.ctx.moveTo(this.prevX, this.prevY)
    this.ctx.lineTo(this.currX, this.currY)
    this.ctx.strokeStyle = "black"
    this.ctx.lineWidth = 2
    this.ctx.stroke()
    this.ctx.closePath()
  


var canvas = new SignTool()
canvas 
  position: absolute;
  border: 2px solid;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<canvas id="canvas"  ></canvas>

【讨论】:

【参考方案9】:

这个问题被问到并得到回答已经有好几年了。

对于任何寻找简单绘图画布的人(例如,从用户/客户那里获取签名),我在这里发布当前接受的答案的更简化的 jquery 版本

$(document).ready(function() 
  var flag, dot_flag = false,
	prevX, prevY, currX, currY = 0,
	color = 'black', thickness = 2;
  var $canvas = $('#canvas');
  var ctx = $canvas[0].getContext('2d');

  $canvas.on('mousemove mousedown mouseup mouseout', function(e) 
    prevX = currX;
    prevY = currY;
    currX = e.clientX - $canvas.offset().left;
    currY = e.clientY - $canvas.offset().top;
    if (e.type == 'mousedown') 
      flag = true;
    
    if (e.type == 'mouseup' || e.type == 'mouseout') 
      flag = false;
    
    if (e.type == 'mousemove') 
      if (flag) 
        ctx.beginPath();
        ctx.moveTo(prevX, prevY);
        ctx.lineTo(currX, currY);
        ctx.strokeStyle = color;
        ctx.lineWidth = thickness;
        ctx.stroke();
        ctx.closePath();
      
    
  );

  $('.canvas-clear').on('click', function(e) 
    c_width = $canvas.width();
    c_height = $canvas.height();
    ctx.clearRect(0, 0, c_width, c_height);
    $('#canvasimg').hide();
  );
);
<html>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
  <body>
    <canvas id="canvas"   style="position:absolute;top:10%;left:10%;border:2px solid;"></canvas>
    <input type="button" value="Clear" class="canvas-clear" />
  </body>
</html>

【讨论】:

【参考方案10】:

一个超短的版本,here,没有 position:absolute 的原生 JavaScript。主要思想是将画布的上下文移动到正确的坐标并画一条线。取消注释click 处理程序并在下面评论mousedownmousemove 处理程序以了解它是如何工作的。

<!DOCTYPE html>
<html>
<body>

<p style="margin: 50px">Just some padding in y direction</p>

<canvas id="myCanvas"   style="background: #000; margin-left: 100px;">Your browser does not support the HTML5 canvas tag.</canvas>

<script>
    const c = document.getElementById("myCanvas");
    // c.addEventListener("click", penTool); // fires after mouse left btn is released
    c.addEventListener("mousedown", setLastCoords); // fires before mouse left btn is released
    c.addEventListener("mousemove", freeForm);


    const ctx = c.getContext("2d");

    function setLastCoords(e) 
        const x, y = c.getBoundingClientRect();
        lastX = e.clientX - x;
        lastY = e.clientY - y;
    

    function freeForm(e) 
        if (e.buttons !== 1) return; // left button is not pushed yet
        penTool(e);
    

    function penTool(e) 
        const x, y = c.getBoundingClientRect();
        const newX = e.clientX - x;
        const newY = e.clientY - y;

        ctx.beginPath();
        ctx.lineWidth = 5;
        ctx.moveTo(lastX, lastY);
        ctx.lineTo(newX, newY);
        ctx.strokeStyle = 'white';
        ctx.stroke();
        ctx.closePath();

        lastX = newX;
        lastY = newY;
    

    let lastX = 0;
    let lastY = 0;  
</script>

</body>
</html>

【讨论】:

重要的是,不要在 CSS 中设置宽度和高度,因为它只会拉伸画布而不是调整其大小 - ***.com/a/8693791/1585523【参考方案11】:

Alco 检查这个: 示例:https://github.com/williammalone/Simple-HTML5-Drawing-App

文档:http://www.williammalone.com/articles/create-html5-canvas-javascript-drawing-app/

本文档包括以下代码:-

HTML:

<canvas id="canvas"  ></canvas>

JS:

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

$('#canvas').mousedown(function(e)
  var mouseX = e.pageX - this.offsetLeft;
  var mouseY = e.pageY - this.offsetTop;

  paint = true;
  addClick(e.pageX - this.offsetLeft, e.pageY - this.offsetTop);
  redraw();
);

$('#canvas').mouseup(function(e)
  paint = false;
);

$('#canvas').mouseleave(function(e)
  paint = false;
);

var clickX = new Array();
var clickY = new Array();
var clickDrag = new Array();
var paint;

function addClick(x, y, dragging)

  clickX.push(x);
  clickY.push(y);
  clickDrag.push(dragging);


//Also redraw
function redraw()
  context.clearRect(0, 0, context.canvas.width, context.canvas.height); // Clears the canvas

  context.strokeStyle = "#df4b26";
  context.lineJoin = "round";
  context.lineWidth = 5;

  for(var i=0; i < clickX.length; i++)         
    context.beginPath();
    if(clickDrag[i] && i)
      context.moveTo(clickX[i-1], clickY[i-1]);
     else
       context.moveTo(clickX[i]-1, clickY[i]);
     
     context.lineTo(clickX[i], clickY[i]);
     context.closePath();
     context.stroke();
  

还有一个很棒的例子http://perfectionkills.com/exploring-canvas-drawing-techniques/

【讨论】:

【参考方案12】:

我使用了 1083202 所做的,但删除了所有控件,并实施了 KWILLIAMS 建议的更改,使其对滚动不敏感。我还把画布做得很大,基本上在我的整个页面上 2000x1600 像素,除了边距。我确实删除了所有绘图工具和按钮,并使用“蓝色”作为唯一颜色。

我将 JS 代码放在一个名为 myJS.js 的单独文件中,并将其放在一个名为“JS”的文件夹中:

然后我用笔记本电脑上的手写笔在触控板上写字,女巫比用手指或鼠标更好地工作。

我使用它的文档是一种非正式的,供内部使用,但是在制作 pdf 文件之前能够在上面画一些笔触是很好的。

    var canvas, ctx, flag = false,
        prevX = 0,
        currX = 0,
        prevY = 0,
        currY = 0,
        dot_flag = false;

    var x = "blue",
        y = 3;
    
    function init() 
        canvas = document.getElementById('can');
        ctx = canvas.getContext("2d");
        w = canvas.width;
        h = canvas.height;
    
        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);
    
 
    function draw() 
        ctx.beginPath();
        ctx.moveTo(prevX, prevY);
        ctx.lineTo(currX, currY);
        ctx.strokeStyle = x;
        ctx.lineWidth = y;
        ctx.stroke();
        ctx.closePath();
    
   
    function findxy(res, e) 
        if (res == 'down') 
            prevX = currX;
            prevY = currY;
            currX = e.clientX - canvas.getBoundingClientRect().left;
            currY = e.clientY - canvas.getBoundingClientRect().top;
    
            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.getBoundingClientRect().left;
                currY = e.clientY - canvas.getBoundingClientRect().top;
                draw();
            
        
    
<html> 



<body onload="init()">
<p>Below you can draw:</p>
<canvas id="can"   style="position:absolute;">Below you can write:</canvas>
</body>

【讨论】:

【参考方案13】:

如果您在执行此操作时遇到问题,请告诉我。它使用 processing.js 并具有更改颜色和使绘制点越来越大的功能。

<html>
<head>
<!--script librarires-->
<script type="text/javascript" src="processing.js"></script>
<script type="text/javascript" src="init.js"></script>

<!--styles -->
<style type="text/css" src="stylesheet.css">
</style> 
</head>
<body>
<!--toolbox -->
<div id="draggable toolbox"></div>
<script type="application/processing">
// new script
int prevx, prevy;
int newx, newy;
boolean cliked;
color c1 = #000000;
int largeur=2;
int ps = 20;
int px = 50;
int py = 50;

void setup() 
size(500,500);
frameRate(25);
background(50);

 prevx = mouseX;
 prevy = mouseY;
 cliked = false;
 

void draw() 
 //couleur
 noStroke(0);
 fill(#FFFFFF);//blanc
rect(px, py, ps, ps);
 fill(#000000);
 rect(px, py+(ps), ps, ps);
 fill(#FF0000);
 rect(px, py+(ps*2), ps, ps);
 fill(#00FF00);
 rect(px, py+(ps*3), ps, ps);
 fill(#FFFF00);
 rect(px, py+(ps*4), ps, ps);
 fill(#0000FF);
 rect(px, py+(ps*5), ps, ps);
 //largeur
 fill(#FFFFFF);
 rect(px, py+(ps*7), ps, ps);
  fill(#FFFFFF);
 rect(px, py+(ps*8), ps, ps);
 stroke(#000000);
 line(px+2, py+(ps*7)+(ps/2), px+(ps-2), py+(ps*7)+(ps/2));
 line(px+(ps/2), py+(ps*7)+1, px+(ps/2), py+(ps*8)-1);
 line(px+2, py+(ps*8)+(ps/2), px+(ps-2), py+(ps*8)+(ps/2));

 if(cliked==false)
  prevx = mouseX;
 prevy = mouseY;  
 
 if(mousePressed)  
  cliked = true;
 newx = mouseX;
  newy = mouseY;
  strokeWeight(largeur);
  stroke(c1);
  line(prevx, prevy, newx, newy);
  prevx = newx;
 prevy = newy;
 else
  cliked= false;
  

void mouseClicked() 
 if (mouseX>=px && mouseX<=(px+ps))
  //couleur
  if (mouseY>=py && mouseY<=py+(ps*6)) 
   c1 = get(mouseX, mouseY);
  
   //largeur
  if (mouseY>=py+(ps*7) && mouseY<=py+(ps*8)) 
   largeur += 2;
  
 if (mouseY>=py+(ps*8) && mouseY<=py+(ps*9)) 
   if (largeur>2)
    largeur -= 2;
   
  
 

</script><canvas></canvas>
</body>
</html>

【讨论】:

我不明白,但它开始处理。【参考方案14】:

如果您的画布有背景图像,则必须进行一些调整才能使其正常工作,因为白色擦除技巧会隐藏背景。

这是带有代码的gist。

<html>
    <script type="text/javascript">
    var canvas, canvasimg, backgroundImage, finalImg;
    var mouseClicked = false;
    var prevX = 0;
    var currX = 0;
    var prevY = 0;
    var currY = 0;
    var fillStyle = "black";
    var globalCompositeOperation = "source-over";
    var lineWidth = 2;

    function init() 
      var imageSrc = '/abstract-geometric-pattern_23-2147508597.jpg'
      backgroundImage = new Image();
      backgroundImage.src = imageSrc;
      canvas = document.getElementById('can');
      finalImg = document.getElementById('finalImg');
      canvasimg = document.getElementById('canvasimg');
      canvas.style.backgroundImage = "url('" + imageSrc + "')";
      canvas.addEventListener("mousemove", handleMouseEvent);
      canvas.addEventListener("mousedown", handleMouseEvent);
      canvas.addEventListener("mouseup", handleMouseEvent);
      canvas.addEventListener("mouseout", handleMouseEvent);
    

    function getColor(btn) 
      globalCompositeOperation = 'source-over';
      lineWidth = 2;
      switch (btn.getAttribute('data-color')) 
        case "green":
        fillStyle = "green";
        break;
        case "blue":
        fillStyle = "blue";
        break;
        case "red":
        fillStyle = "red";
        break;
        case "yellow":
        fillStyle = "yellow";
        break;
        case "orange":
        fillStyle = "orange";
        break;
        case "black":
        fillStyle = "black";
        break;
        case "eraser":
        globalCompositeOperation = 'destination-out';
        fillStyle = "rgba(0,0,0,1)";
        lineWidth = 14;
        break;
      

    

    function draw(dot) 
      var ctx = canvas.getContext("2d");
      ctx.beginPath();
      ctx.globalCompositeOperation = globalCompositeOperation;
      if(dot)
        ctx.fillStyle = fillStyle;
        ctx.fillRect(currX, currY, 2, 2);
       else 
        ctx.beginPath();
        ctx.moveTo(prevX, prevY);
        ctx.lineTo(currX, currY);
        ctx.strokeStyle = fillStyle;
        ctx.lineWidth = lineWidth;
        ctx.stroke();
      
      ctx.closePath();
    

    function erase() 
      if (confirm("Want to clear")) 
        var ctx = canvas.getContext("2d");
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        document.getElementById("canvasimg").style.display = "none";
      
    

    function save() 
      canvas.style.border = "2px solid";
      canvasimg.width = canvas.width;
      canvasimg.height = canvas.height;
      var ctx2 = canvasimg.getContext("2d");
      // comment next line to save the draw only
      ctx2.drawImage(backgroundImage, 0, 0);
      ctx2.drawImage(canvas, 0, 0);
      finalImg.src = canvasimg.toDataURL();
      finalImg.style.display = "inline";
    

    function handleMouseEvent(e) 
      if (e.type === 'mousedown') 
        prevX = currX;
        prevY = currY;
        currX = e.offsetX;
        currY = e.offsetY;
        mouseClicked = true;
        draw(true);
      
      if (e.type === 'mouseup' || e.type === "mouseout") 
        mouseClicked = false;
      
      if (e.type === 'mousemove') 
        if (mouseClicked) 
          prevX = currX;
          prevY = currY;
          currX = e.offsetX;
          currY = e.offsetY;
          draw();
        
      
    
    </script>
    <body onload="init()">
      <canvas id="can"   style="position:absolute;top:10%;left:10%;border:2px solid;">
      </canvas>
      <div style="position:absolute;top:12%;left:43%;">Choose Color</div>
      <div style="position:absolute;top:15%;left:45%;width:10px;height:10px;background:green;" data-color="green" onclick="getColor(this)"></div>
      <div style="position:absolute;top:15%;left:46%;width:10px;height:10px;background:blue;" data-color="blue" onclick="getColor(this)"></div>
      <div style="position:absolute;top:15%;left:47%;width:10px;height:10px;background:red;" data-color="red" onclick="getColor(this)"></div>
      <div style="position:absolute;top:17%;left:45%;width:10px;height:10px;background:yellow;" data-color="yellow" onclick="getColor(this)"></div>
      <div style="position:absolute;top:17%;left:46%;width:10px;height:10px;background:orange;" data-color="orange" onclick="getColor(this)"></div>
      <div style="position:absolute;top:17%;left:47%;width:10px;height:10px;background:black;" data-color="black" onclick="getColor(this)"></div>
      <div style="position:absolute;top:20%;left:43%;">Eraser</div>
      <div style="position:absolute;top:22%;left:45%;width:15px;height:15px;background:white;border:2px solid;" data-color="eraser" onclick="getColor(this)"></div>
      <canvas id="canvasimg" style="display:none;" ></canvas>
      <img id="finalImg" style="position:absolute;top:10%;left:52%;display:none;" >
      <input type="button" value="save" id="btn" size="30" onclick="save()" style="position:absolute;top:55%;left:10%;">
      <input type="button" value="clear" id="clr" size="23" onclick="erase()" style="position:absolute;top:55%;left:15%;">
    </body>
    </html>

【讨论】:

以上是关于使用鼠标在 HTML5 Canvas 上绘图的主要内容,如果未能解决你的问题,请参考以下文章

canvas 鼠标绘图

你好 你的这个问题 “ html5 canvas 中用鼠标画(拉出)直线的问题! ”

html5 canvas绘图有啥用

html5的canvas绘制了个房间结构图,鼠标移动到哪个房间,哪个房间填充颜色

SVG 与 HTML5 的 canvas 各有啥优点,哪个更有前途

用html5的canvas和JavaScript创建一个绘图程序