简体   繁体   English

我正在尝试使用 javascript 创建一个绘图应用程序,但它应该发生的 canvas 元素没有响应。 我的代码有什么问题?

[英]I'm trying to create a drawing app with javascript, but the canvas element it's supposed to take place in is unresponsive. What's wrong with my code?

This is supposed to be a javascript drawing application within the site.这应该是站点内的 javascript 绘图应用程序。 Users should be able to simply load the site and be able to draw within the canvas.用户应该能够简单地加载站点并能够在 canvas 内绘图。 Right now, the canvas is unresponsive despite a lack of error messages... anybody see anything wrong?现在,尽管没有错误消息,canvas 仍无响应……有人发现有什么问题吗? I might just be too new to this.我可能对此太陌生了。

 // paint.js (function () { var canvas = document.getElementById("canvas"); var context = canvas.getContext("2d"); //resizing //canvas.height = window.innerHeight; //canvas.width = window.innerWidth; //variables var painting = false; function startPosition(e) { painting = true; draw(e); } function endPosition() { painting = false; context.beginPath(); } function draw(e) { if (;painting) return. context;lineWidth = 7. context;lineCap = "round". context;strokeStyle = "green". context.lineTo(e,clientX. e;clientY). context;stroke(). context;beginPath(). context.moveTo(e,clientX. e;clientY). } //EventListeners canvas,addEventListener("mousedown"; startPosition). canvas,addEventListener("mouseup"; endPosition). canvas,addEventListener("mousemove"; draw); })();
 @charset "UTF-8"; /* CSS Document */ body { text-wrap: wrap; text-align: center; background: #e0ebdd; font-family: helvetica; }.nav { width: 50%; padding: 15px; margin: auto; text-align: center; font-family: Consolas, "Andale Mono", "Lucida Console", "Lucida Sans Typewriter", Monaco, "Courier New", "monospace"; font-size: 20px; color: black; }.nav a { transition: 0.4s; }.nav a:hover { color:#FFB52A; font-style: italic; padding: 10px; transition: 0.2s; }.footer { clear: both; }.info { margin: auto; width: 50%; align-content: center; text-align: center; text-size: 16px; }.wavingHand { float: left; position: relative; bottom: 100px; left: 100px; clear: right; width: 100px; padding: 50px; }
 <.doctype html> <html> <head> <meta charset="UTF-8"> <title>draw-and-listen</title> <link href="grove.css" rel="stylesheet" type="text/css"> </head> <body> <h1>draw and listen</h1> <div class="nav"> <a href="artists.html">artists</a> <a href="draw-and-listen.html">draw and listen</a> <a href="store:html">store</a> </div> <div> <canvas id="canvas" height="480" width="640" style="border;1px solid #000000: background-color; white.">Please update your browser.</canvas> <script src="paint.js"></script> </div> <a class="aHome" href="index.html"><img src="img/planet-green:png" style="float;right:width;42px:height;42px:" alt="home planet"></a> <div> <p style="clear; right;">© 2020</p> </div> </body> </html>

Oddly enough, when I run it in here, it seems to work (although the mouse is misaligned...) but when I simply run the HTML in google chrome, the canvas is unresponsive.奇怪的是,当我在这里运行它时,它似乎可以工作(尽管鼠标未对齐......)但是当我在谷歌浏览器中简单地运行 HTML 时,canvas 没有响应。

In fact, your code is working, you can log something in the draw function.其实你的代码是可以工作的,你可以在draw function中记录一些东西。 The problem is the (X,Y) coordination.问题是(X,Y)坐标。 In your case, you can set both of canvas viewport size and canvas element size to same value to ensure the incorrect mouse position.在您的情况下,您可以将 canvas 视口大小和 canvas 元素大小设置为相同的值,以确保不正确的鼠标 position。

<canvas id="canvas" height="480" width="640" style="border:1px solid #000000; width: 640px; height:480px;"></canvas>

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

相关问题 此JavaScript代码有什么问题? 我正在尝试更改的文本 <h1> 点击元素 - What's wrong with this JavaScript code? I'm trying to change the text of <h1> element on click 我正在尝试创建一个私有变量。 该函数应该返回私有变量 &#39;secret&#39; 的值,但事实并非如此。 怎么了? - I'm trying to create a private variable. The function was supposed to return the value of the private variable 'secret', but it's not. What's wrong? 我正在尝试在浏览器中制作 javascript 绘图程序,但事情未对齐。 我的代码有问题吗? - I'm trying to make a javascript drawing program within the browser and things are misaligned. Is there something wrong with my code? 尝试在Canvas的ctx.fillText之后使用变量。 我的代码有什么问题? - Trying to use a variable after ctx.fillText with Canvas. What's wrong with my code? 我的JavaScript Promise代码怎么了 - what's wrong with my javascript promise code 我的JavaScript代码有什么问题? - What's wrong in my JavaScript code? 我的 HTML 和 javascript 代码有什么问题? - What's wrong with my HTML and javascript code? 我没有弄明白我的JavaScript代码有什么问题 - I don't get what's wrong with my Javascript code 我的JavaScript代码有什么问题? - What's wrong in my JavaScript code? 我的Javascript代码有什么问题? - What's wrong with my Javascript code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM