javascript - 如何解决这个意外的令牌错误?
问题描述
我正在尝试制作一个非常简单的绘图应用程序,但我在语法方面遇到了真正的麻烦......
我只希望用户能够在画布上画一条线。
// paint.js
window.onload = 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);
};
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>draw</title>
<link href="grove.css" rel="stylesheet" type="text/css">
</head>
<body>
<h1>draw</h1>
<div class="nav">
<a href="artists.html">artists</a>
<a href="draw.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>
有完整的 HTML 和 js 文件提供更多上下文来尝试解决画布在网页中没有响应的问题。它似乎可以作为堆栈溢出中的独立函数正常工作,这让我相信其他代码的问题......
解决方案
推荐阅读
- javascript - 关闭它的父窗口时,事件侦听器不在弹出窗口上工作
- python - 浏览网页的最佳(最快)方法是什么?
- laravel - 如何根据字段值从表中获取前 10 行?
- linux - OpenVPN 客户端无法在 Windows 计算机上运行?
- c# - 无法将具有一对一关系的模型与 Entity Framework Core 映射
- c# - 尝试为自定义 POCO 的简单列表获取 HashCode
- java - 从 Spring Boot 重定向角度页面时出现 CORS 错误:预检中 Access-Control-Allow-Headers 不允许标头字段内容类型
- python - 合并共享一个单元格值的 Python 3 DataFrame 行,将另一个值放入逗号分隔的数组中
- css - 猫头鹰旋转木马最后会产生空白空间
- ruby-on-rails - Axios post 工作,Axios put 没有通过预加载请求