Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I hand draw on canvas with JavaScript?

Question

How do I draw free (using my mouse / fingers) on a canvas element like you can do it in paint with a pencil?

About this question

There are a lot of questions that want to achieve free hand drawing on canvas:

  • draw by mouse with HTML5 Canvas
  • KineticJS - Draw free with mouse
  • Free drawing on canvas using fabric.js
  • Sketching with JS
  • Paint canvas not working properly
  • Mouse position on canvas painting
  • Implementing smooth sketching and drawing on the element

So I thought it would be a good idea to make a reference question, where every answer is community wiki and contains a explanation for exactly one JavaScript library / pure JavaScript how to do paint on canvas.

Structure of answers

The answers should be community wiki and use the following template:

## [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 
like image 461
Martin Thoma Avatar asked Apr 06 '14 08:04

Martin Thoma


People also ask

How to draw on the HTML Canvas with JavaScript?

Drawing on the HTML canvas is to be done with JavaScript. Use the HTML DOM Method getElementById () and getContext () before drawing on the canvas. You need to use the getElementById () method to find the canvas element. Use the getContext (), which is drawing object for the canvas.

How to draw on canvas in AutoCAD?

1 Find the Canvas Element First of all, you must find the <canvas> element. This is done by using the HTML DOM method getElementById (): var canvas = document.getElementById("myCanvas"); 2 Create a Drawing Object Secondly, you need a drawing object for the canvas. ... 3 Draw on the Canvas

How to create a drawing application with canvas?

@TaylorA.Leach yep, if canvas is not placed in top right corner, you will need to add some offset in setPosition function... Here's the most straightforward way to create a drawing application with canvas: Attach a mousedown, mousemove, and mouseup event listener to the canvas DOM

How do I draw on canvas with getcontext?

The getContext () is a built-in HTML object, with properties and methods for drawing: Finally, you can draw on the canvas. The fillStyle property can be a CSS color, a gradient, or a pattern. The default fillStyle is black.


1 Answers

Fabric.js

<!DOCTYPE html> <html>   <head>     <title>Simple example</title>     <script type='text/javascript' src='http://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.4.0/fabric.min.js'></script>     <style type='text/css'>         #sheet {             border:1px solid black;         }     </style>     <script type='text/javascript'>         window.onload=function(){             var canvas = new fabric.Canvas('sheet');             canvas.isDrawingMode = true;             canvas.freeDrawingBrush.width = 5;             canvas.freeDrawingBrush.color = "#ff0000";         }     </script>   </head>   <body>     <canvas id="sheet" width="400" height="400"></canvas>   </body> </html> 

JSFiddle - Demo

  • The width of the lines can be controlled with canvas.freeDrawingBrush.width.
  • The color of the lines can be controlled with canvas.freeDrawingBrush.color.

This solution works with:

  • Desktop computers:
    • Chrome 33
    • Firefox 28
  • Touch devices:
    • Chrome 34 on Nexus 4
    • Opera 20 on Nexus 4
    • Firefox 28 on Nexus 4

Import / Export

Is only possible by serializing the complete canvas, see Tutorial

Line smoothing

Is done automatically and it seems not to be possible to deactivate it.

like image 104
6 revs, 3 users 98% Avatar answered Oct 17 '22 21:10

6 revs, 3 users 98%