问题
如何在画布元素上自由地(使用鼠标/手指)绘制,就像在画图中用铅笔一样?
关于此问题
有很多问题想在画布上实现自由绘制:
- 使用 HTML5 画布和鼠标绘图
- KineticJS - 使用鼠标自由绘图
- 使用 fabric.js 在画布上自由绘制
- 使用 JS 进行素描
- 画布绘制不正常
- 画布绘制时鼠标位置
- 在 <canvas> 元素上实现平滑素描和绘制
因此我认为制作一个参考问题是个好主意,每个答案都是社区 wiki,并包含一个 JavaScript 库或纯 JavaScript 如何在画布上绘制的解释。
答案结构
答案应该是社区 wiki,并使用以下模板:
## [Name of library](Link to project page)
### Simple example
A basic, complete example. That means it has to contain HTML
and JavaScript. You can start with this:
<!DOCTYPE html>
<html>
<head>
<title>Simple example</title>
<script type='text/javascript' src='http://cdnjs.com/[your library]'></script>
<style type='text/css'>
#sheet {
border:1px solid black;
}
</style>
<script type='text/javascript'>
window.onload=function(){
// TODO: Adjust
}
</script>
</head>
<body>
<canvas id="sheet" width="400" height="400"></canvas>
</body>
</html>
If possible, this example should work with both, mouse and touch events.
[JSFiddle](Link to code on jsfiddle.net)
This solution works with:
<!-- Please test it the following way: Write "Hello World"
Problems that you test this way are:
* Does it work at all?
* Are lines separated?
* Does it get slow when you write too much?
-->
* Desktop computers:
* [Browser + Version list]
* Touch devices:
* [Browser + Version list] on [Device name]
### Import / Export
Some explanations how to import / export user drawn images.
### Line smoothing
Explanations about how to manipulate the line the user draws.
This can include:
* Bézier curves
* Controlling thickness of lines