asimryu

canvas draw line

Sep 25th, 2014
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>선그리기</title>
  6. </head>
  7. <body>
  8. <canvas id="c" width="500" height="500" style="border:1px solid #ccc"></canvas>
  9. <script>
  10. var canvas = new fabric.Canvas('c', { selection: false });
  11. var line, isDown;
  12. canvas.on('mouse:down', function(o){
  13. isDown = true;
  14. var pointer = canvas.getPointer(o.e);
  15. var points = [ pointer.x, pointer.y, pointer.x, pointer.y ];
  16. line = new fabric.Line(points, {
  17. strokeWidth: 5,
  18. fill: 'red',
  19. stroke: 'red',
  20. originX: 'center',
  21. originY: 'center'
  22. });
  23. canvas.add(line);
  24. });
  25. canvas.on('mouse:move', function(o){
  26. if (!isDown) return;
  27. var pointer = canvas.getPointer(o.e);
  28. line.set({ x2: pointer.x, y2: pointer.y });
  29. canvas.renderAll();
  30. });
  31. canvas.on('mouse:up', function(o){
  32. isDown = false;
  33. });
  34. </script>
  35. </body>
  36. </html>
Advertisement
Add Comment
Please, Sign In to add comment