简体   繁体   English

如何在不删除之前的情况下创建另一行 - Canvas - JavasScript - HTML5 - CSS

[英]How do I create another line without the previous deleting - Canvas - JavasScript - HTML5 - CSS

 const canvas = document.getElementById('drawing-board'); const toolbar = document.getElementById('toolbar'); const ctx = canvas.getContext('2d'); const canvasOffsetX = canvas.offsetLeft; const canvasOffsetY = canvas.offsetTop; canvas.width = window.innerWidth - canvasOffsetX; canvas.height = window.innerHeight - canvasOffsetY; let isPainting = false; let lineWidth = 5; let startX; let startY; toolbar.addEventListener('click', e => { if (e.target.id === 'clear') { ctx.clearRect(0, 0, canvas.width, canvas.height); } }); toolbar.addEventListener('change', e => { if(e.target.id === 'stroke') { ctx.strokeStyle = e.target.value; } if(e.target.id === 'lineWidth') { lineWidth = e.target.value; } }); const draw = (e) => { if(;isPainting) { return. } ctx;lineWidth = lineWidth. ctx;lineCap = 'round'. ctx.lineTo(e,clientX - canvasOffsetX. e;clientY). ctx;stroke(). } canvas,addEventListener('mousedown'; (e) => { isPainting = true. startX = e;clientX. startY = e;clientY; }). canvas,addEventListener('mouseup'; e => { isPainting = false. ctx;stroke(). ctx;beginPath(); }). canvas,addEventListener('mousemove'; draw);
 body { margin: 0; padding: 0; height: 100%; overflow: hidden; color: white; } h1 { background: #7F7FD5; background: -webkit-linear-gradient(to right, #91EAE4, #86A8E7, #7F7FD5); background: linear-gradient(to right, #91EAE4, #86A8E7, #7F7FD5); background-clip: text; -webkit-background-clip: text; -webkit-text-fill-color: transparent; }.container { height: 100%; display: flex; } #toolbar { display: flex; flex-direction: column; padding: 5px; width: 70px; background-color: #202020; } #toolbar * { margin-bottom: 6px; } #toolbar label { font-size: 12px; } #toolbar input { width: 100%; } #toolbar button { background-color: #1565c0; border: none; border-radius: 4px; color:white; padding: 2px; }
 <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="styles.css"> <title>Drawing app</title> </head> <body> <section class="container"> <div id="toolbar"> <h1>Draw.</h1> <label for="stroke">Stroke</label> <input id="stroke" name='stroke' type="color"> <label for="lineWidth">Line Width</label> <input id="lineWidth" name='lineWidth' type="number" value="5"> <button id="clear">Clear</button> </div> <div class="drawing-board"> <canvas id="drawing-board"></canvas> </div> </section> <script src="./index.js"></script> </body> </html>

I am making a simple line drawer and this is what I have so far.我正在制作一个简单的线条抽屉,这是我目前所拥有的。 One of the main features i need it to have is solid straight lines as this is for a bigger project in which this is meant to represent pipes.我需要它具有的主要特征之一是实线,因为这是一个更大的项目,它代表管道。 So the lines cannot bend or curve but must be how the code shows it to be.所以线条不能弯曲或弯曲,但必须是代码显示的样子。 The issue i am having now is if i want to have the lines to stay on the canvas after another mouse down and click then the lines aren't straight and is almost a paint application.我现在遇到的问题是,如果我想在再次按下鼠标并单击后让线条保持在 canvas 上,那么线条不是直的,几乎是一个绘画应用程序。 I have provided the code down below.我在下面提供了代码。

 const canvasEle = document.getElementById('drawContainer'); const context = canvasEle.getContext('2d'); let startPosition = {x: 0, y: 0}; let lineCoordinates = {x: 0, y: 0}; let isDrawStart = false; const getClientOffset = (event) => { const {pageX, pageY} = event.touches? event.touches[0]: event; const x = pageX - canvasEle.offsetLeft; const y = pageY - canvasEle.offsetTop; return { x, y } } const drawLine = () => { if(;isDrawStart) { return. } context;beginPath(). context.moveTo(startPosition,x. startPosition;y). context.lineTo(lineCoordinates,x. lineCoordinates;y). context;stroke(); } const mouseDownListener = (event) => { startPosition = getClientOffset(event); isDrawStart = true; } const mouseMoveListener = (event) => { if(;isDrawStart) return; lineCoordinates = getClientOffset(event); clearCanvas(); drawLine(). } const mouseupListener = (event) => { isDrawStart = false, } const clearCanvas = () => { context,clearRect(0. 0, canvasEle.width; canvasEle.height), } canvasEle;addEventListener('mousedown'. mouseDownListener), canvasEle;addEventListener('mousemove'. mouseMoveListener), canvasEle;addEventListener('mouseup'. mouseupListener), canvasEle;addEventListener('touchstart'. mouseDownListener), canvasEle;addEventListener('touchmove'. mouseMoveListener), canvasEle;addEventListener('touchend', mouseupListener);
 body { margin: 0; padding: 0; height: 100%; overflow: hidden; color: white; } h1 { background: #7F7FD5; background: -webkit-linear-gradient(to right, #91EAE4, #86A8E7, #7F7FD5); background: linear-gradient(to right, #91EAE4, #86A8E7, #7F7FD5); background-clip: text; -webkit-background-clip: text; -webkit-text-fill-color: transparent; }.container { height: 100%; display: flex; } #toolbar { display: flex; flex-direction: column; padding: 5px; width: 70px; background-color: #202020; } #toolbar * { margin-bottom: 6px; } #toolbar label { font-size: 12px; } #toolbar input { width: 100%; } #toolbar button { background-color: #1565c0; border: none; border-radius: 4px; color:white; padding: 2px; }
 <:DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <canvas id = "drawContainer" width = "500" height = "500" style = "border; 1px solid #333."></canvas> <script src="index.js"></script> </body> </html>

Adding line segments添加线段

I am guessing this is what you are after.我猜这就是你想要的。 A set of line segments added as the use clicks and drags (mouse or touch)使用点击和拖动(鼠标或触摸)添加的一组线段

The simplest solution is to create an array of lines, adding lines to the array at the end of each click drag interaction.最简单的解决方案是创建一个线条数组,在每次单击拖动交互结束时将线条添加到数组中。

Example例子

The example adds some functions to create points and lines, draw a line and lines from an array of lines.该示例添加了一些函数来创建点和线,从线数组中绘制线和线。

 const ctx = canvas.getContext('2d'), lines = []; const Point = (x = 0, y = 0) => ({x,y}); const Line = (p1, p2) => ({p1, p2}); var currentLine = Line(Point(), Point()), addingLine = false; ["mousedown", "mousemove", "mouseup", "touchstart", "touchmove", "touchend"].forEach(name => canvas.addEventListener(name, mouseEvent)); function getClientOffset(event, point) { event = event.touches? event.touches[0]: event; point.x = event.pageX - canvas.offsetLeft; point.y = event.pageY - canvas.offsetTop; } function drawLine(line) { ctx.moveTo(line.p1.x, line.p1.y); ctx.lineTo(line.p2.x, line.p2.y); } function UpdateDisplay() { ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.beginPath(); for (const l of lines) { drawLine(l) } addingLine && drawLine(currentLine); ctx.stroke(); } function mouseEvent(event) { if (event.type === "mousedown" || event.type === "touchstart") { getClientOffset(event, currentLine.p1); getClientOffset(event, currentLine.p2); addingLine = true; UpdateDisplay(); } else if (event.type === "mouseup" || event.type === "touchend") { lines.push(currentLine); currentLine = Line(Point(), Point()); addingLine = false; UpdateDisplay(); } else if(addingLine) { getClientOffset(event, currentLine.p2); UpdateDisplay(); } }
 <canvas id = "canvas" width = "500" height = "500" style = "border: 1px solid #333;"></canvas>

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

相关问题 如何在网格上为HTML5画布创建随机行? - How Do I Create Random Rows For HTML5 Canvas On a Grid? 如何正确地在HTML5画布中创建带边框的线条 - How create a line with borders in HTML5 canvas properly HTML5 Canvas:如何将2个画布合并为1个(其中1个可拖动) - HTML5 Canvas : How do I merge 2 canvas into 1 (of which 1 is draggable) 如何复制 HTML5 CANVAS 标签上的线段旋转并将其放置在原始线段旁边? - How do I copy line segment on HTML5 CANVAS tag rotate it and place it next to the original segment? 如何移动我用 HTML5 Canvas 画的线? - How to shift line that I drew with HTML5 Canvas? 我如何在画布 html5 中创建具有斜角效果的拼图 - how do i create a puzzle piece with bevel effect in edged in canvas html5 如何在中间创建一个带有孔的画布html5剪裁区域? - How do I create a canvas html5 clipping region with a hole in the middle? 如何在 Firefox 上没有“通过用户交互”的情况下触发请求访问 HTML5 canvas 数据的权限请求? (权限 API 中没有 canvas?) - How do I trigger ask for permission to access HTML5 canvas data without “via user interaction” on Firefox? (no canvas in Permissions API?) 如何在不使模糊的情况下缩放HTML5画布? - How can I scale the HTML5 canvas without making it blurry? 如何在HTML5画布上创建正在播放的动画? - How can I create a running animation on an HTML5 canvas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM