Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.89 KB | None | 0 0
  1. ## [Name of library](Link to project page)
  2. ### Simple example
  3. A basic, complete example. That means it has to contain HTML
  4. and JavaScript. You can start with this:
  5.  
  6. <!DOCTYPE html>
  7. <html>
  8. <head>
  9. <title>Simple example</title>
  10. <script type='text/javascript' src='http://cdnjs.com/[your library]'></script>
  11. <style type='text/css'>
  12. #sheet {
  13. border:1px solid black;
  14. }
  15. </style>
  16. <script type='text/javascript'>
  17. window.onload=function(){
  18. // TODO: Adjust
  19. }
  20. </script>
  21. </head>
  22. <body>
  23. <canvas id="sheet" width="400" height="400"></canvas>
  24. </body>
  25. </html>
  26.  
  27. If possible, this example should work with both, mouse and touch events.
  28.  
  29. [JSFiddle](Link to code on jsfiddle.net)
  30.  
  31. This solution works with:
  32.  
  33. <!-- Please test it the following way: Write "Hello World"
  34. Problems that you test this way are:
  35. * Does it work at all?
  36. * Are lines separated?
  37. * Does it get slow when you write too much?
  38. -->
  39.  
  40. * Desktop computers:
  41. * [Browser + Version list]
  42. * Touch devices:
  43. * [Browser + Version list] on [Device name]
  44.  
  45. ### Import / Export
  46. Some explanations how to import / export user drawn images.
  47.  
  48. ### Line smoothing
  49. Explanations about how to manipulate the line the user draws.
  50. This can include:
  51. * Bézier curves
  52. * Controlling thickness of lines
  53.  
  54. <!DOCTYPE html>
  55. <html>
  56. <head>
  57. <title>Simple example</title>
  58. <style type='text/css'>
  59. #sheet {
  60. border:1px solid black;
  61. }
  62. </style>
  63. </head>
  64. <body>
  65. <canvas id="sheet" width="400" height="400"></canvas>
  66. <script type='text/javascript'>
  67. /*jslint browser:true */
  68. "use strict";
  69. var context = document.getElementById('sheet').getContext("2d");
  70. var canvas = document.getElementById('sheet');
  71. context = canvas.getContext("2d");
  72. context.strokeStyle = "#ff0000";
  73. context.lineJoin = "round";
  74. context.lineWidth = 5;
  75.  
  76. var clickX = [];
  77. var clickY = [];
  78. var clickDrag = [];
  79. var paint;
  80.  
  81. /**
  82. * Add information where the user clicked at.
  83. * @param {number} x
  84. * @param {number} y
  85. * @return {boolean} dragging
  86. */
  87. function addClick(x, y, dragging) {
  88. clickX.push(x);
  89. clickY.push(y);
  90. clickDrag.push(dragging);
  91. }
  92.  
  93. /**
  94. * Redraw the complete canvas.
  95. */
  96. function redraw() {
  97. // Clears the canvas
  98. context.clearRect(0, 0, context.canvas.width, context.canvas.height);
  99.  
  100. for (var i = 0; i < clickX.length; i += 1) {
  101. if (!clickDrag[i] && i == 0) {
  102. context.beginPath();
  103. context.moveTo(clickX[i], clickY[i]);
  104. context.stroke();
  105. } else if (!clickDrag[i] && i > 0) {
  106. context.closePath();
  107.  
  108. context.beginPath();
  109. context.moveTo(clickX[i], clickY[i]);
  110. context.stroke();
  111. } else {
  112. context.lineTo(clickX[i], clickY[i]);
  113. context.stroke();
  114. }
  115. }
  116. }
  117.  
  118. /**
  119. * Draw the newly added point.
  120. * @return {void}
  121. */
  122. function drawNew() {
  123. var i = clickX.length - 1
  124. if (!clickDrag[i]) {
  125. if (clickX.length == 0) {
  126. context.beginPath();
  127. context.moveTo(clickX[i], clickY[i]);
  128. context.stroke();
  129. } else {
  130. context.closePath();
  131.  
  132. context.beginPath();
  133. context.moveTo(clickX[i], clickY[i]);
  134. context.stroke();
  135. }
  136. } else {
  137. context.lineTo(clickX[i], clickY[i]);
  138. context.stroke();
  139. }
  140. }
  141.  
  142. function mouseDownEventHandler(e) {
  143. paint = true;
  144. var x = e.pageX - canvas.offsetLeft;
  145. var y = e.pageY - canvas.offsetTop;
  146. if (paint) {
  147. addClick(x, y, false);
  148. drawNew();
  149. }
  150. }
  151.  
  152. function touchstartEventHandler(e) {
  153. paint = true;
  154. if (paint) {
  155. addClick(e.touches[0].pageX - canvas.offsetLeft, e.touches[0].pageY - canvas.offsetTop, false);
  156. drawNew();
  157. }
  158. }
  159.  
  160. function mouseUpEventHandler(e) {
  161. context.closePath();
  162. paint = false;
  163. }
  164.  
  165. function mouseMoveEventHandler(e) {
  166. var x = e.pageX - canvas.offsetLeft;
  167. var y = e.pageY - canvas.offsetTop;
  168. if (paint) {
  169. addClick(x, y, true);
  170. drawNew();
  171. }
  172. }
  173.  
  174. function touchMoveEventHandler(e) {
  175. if (paint) {
  176. addClick(e.touches[0].pageX - canvas.offsetLeft, e.touches[0].pageY - canvas.offsetTop, true);
  177. drawNew();
  178. }
  179. }
  180.  
  181. function setUpHandler(isMouseandNotTouch, detectEvent) {
  182. removeRaceHandlers();
  183. if (isMouseandNotTouch) {
  184. canvas.addEventListener('mouseup', mouseUpEventHandler);
  185. canvas.addEventListener('mousemove', mouseMoveEventHandler);
  186. canvas.addEventListener('mousedown', mouseDownEventHandler);
  187. mouseDownEventHandler(detectEvent);
  188. } else {
  189. canvas.addEventListener('touchstart', touchstartEventHandler);
  190. canvas.addEventListener('touchmove', touchMoveEventHandler);
  191. canvas.addEventListener('touchend', mouseUpEventHandler);
  192. touchstartEventHandler(detectEvent);
  193. }
  194. }
  195.  
  196. function mouseWins(e) {
  197. setUpHandler(true, e);
  198. }
  199.  
  200. function touchWins(e) {
  201. setUpHandler(false, e);
  202. }
  203.  
  204. function removeRaceHandlers() {
  205. canvas.removeEventListener('mousedown', mouseWins);
  206. canvas.removeEventListener('touchstart', touchWins);
  207. }
  208.  
  209. canvas.addEventListener('mousedown', mouseWins);
  210. canvas.addEventListener('touchstart', touchWins);
  211. </script>
  212. </body>
  213. </html>
  214.  
  215. <!DOCTYPE html>
  216. <html>
  217. <head>
  218. <title>Simple example</title>
  219. <script type='text/javascript' src='http://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.4.0/fabric.min.js'></script>
  220. <style type='text/css'>
  221. #sheet {
  222. border:1px solid black;
  223. }
  224. </style>
  225. <script type='text/javascript'>
  226. window.onload=function(){
  227. var canvas = new fabric.Canvas('sheet');
  228. canvas.isDrawingMode = true;
  229. canvas.freeDrawingBrush.width = 5;
  230. canvas.freeDrawingBrush.color = "#ff0000";
  231. }
  232. </script>
  233. </head>
  234. <body>
  235. <canvas id="sheet" width="400" height="400"></canvas>
  236. </body>
  237. </html>
  238.  
  239. <!DOCTYPE html>
  240. <html>
  241. <head>
  242. <title>Paper.js example</title>
  243. <script type='text/javascript' src='http://paperjs.org/assets/js/paper.js'></script>
  244. <style type='text/css'>
  245. #sheet {
  246. border:1px solid black;
  247. }
  248. </style>
  249. </head>
  250. <body>
  251. <script type="text/paperscript" canvas="sheet">
  252. var path;
  253.  
  254. function onMouseDown(event) {
  255. // If we produced a path before, deselect it:
  256. if (path) {
  257. path.selected = false;
  258. }
  259.  
  260. // Create a new path and set its stroke color to black:
  261. path = new Path({
  262. segments: [event.point],
  263. strokeColor: 'black',
  264. strokeWidth: 3
  265. });
  266. }
  267.  
  268. // While the user drags the mouse, points are added to the path
  269. // at the position of the mouse:
  270. function onMouseDrag(event) {
  271. path.add(event.point);
  272. }
  273.  
  274. // When the mouse is released, we simplify the path:
  275. function onMouseUp(event) {
  276. path.simplify();
  277. }
  278. </script>
  279.  
  280. <canvas id="sheet" width="400" height="400"></canvas>
  281. </body>
  282. </html>
  283.  
  284. <!DOCTYPE html>
  285. <html>
  286. <head>
  287. <title>Simple example</title>
  288. <style type='text/css'>
  289. #sheet {border:1px solid black;}
  290. </style>
  291. </head>
  292. <body>
  293. <canvas id="sheet" width="400" height="400"></canvas>
  294. <script src="http://scrawl.rikweb.org.uk/js/scrawlCore-min.js"></script>
  295. <script>
  296. var mycode = function(){
  297. //define variables
  298. var myPad = scrawl.pad.sheet,
  299. myCanvas = scrawl.canvas.sheet,
  300. sX, sY, here,
  301. drawing = false,
  302. currentSprite = false,
  303. startDrawing,
  304. endDrawing;
  305.  
  306. //event listeners
  307. startDrawing = function(e){
  308. drawing = true;
  309. currentSprite = scrawl.newShape({
  310. start: here,
  311. lineCap: 'round',
  312. lineJoin: 'round',
  313. method: 'draw',
  314. lineWidth: 4,
  315. strokeStyle: 'red',
  316. data: 'l0,0 ',
  317. });
  318. sX = here.x;
  319. sY = here.y;
  320. if(e){
  321. e.stopPropagation();
  322. e.preventDefault();
  323. }
  324. };
  325. myCanvas.addEventListener('mousedown', startDrawing, false);
  326.  
  327. endDrawing = function(e){
  328. if(currentSprite){
  329. currentSprite = false;
  330. }
  331. drawing = false;
  332. if(e){
  333. e.stopPropagation();
  334. e.preventDefault();
  335. }
  336. };
  337. myCanvas.addEventListener('mouseup', endDrawing, false);
  338.  
  339. //animation object
  340. scrawl.newAnimation({
  341. fn: function(){
  342. //get current mouse position
  343. here = myPad.getMouse();
  344. if(here.active){
  345. if(drawing){
  346. if(here.x !== sX || here.y !== sY){
  347. //extend the line
  348. currentSprite.set({
  349. data: currentSprite.data+' '+(here.x - sX)+','+(here.y - sY),
  350. });
  351. sX = here.x;
  352. sY = here.y;
  353. }
  354. }
  355. }
  356. else{
  357. //stop drawing if mouse leaves canvas area
  358. if(currentSprite){
  359. endDrawing();
  360. }
  361. }
  362. //update display
  363. scrawl.render();
  364. },
  365. });
  366. };
  367.  
  368. //Scrawl is modular - load additional modules
  369. scrawl.loadModules({
  370. path: 'js/',
  371. modules: ['animation', 'shape'],
  372. callback: function(){
  373. window.addEventListener('load', function(){
  374. scrawl.init(); //start Scrawl
  375. mycode(); //run code
  376. }, false);
  377. },
  378. });
  379. </script>
  380. </body>
  381. </html>
  382.  
  383. A basic, complete example. That means it has to contain HTML
  384. and JavaScript. You can start with this:
  385.  
  386. <!DOCTYPE html>
  387. <html>
  388. <head>
  389. <title>EaselJS example</title>
  390.  
  391. <script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/EaselJS/0.7.1/easeljs.min.js"></script>
  392.  
  393. <script>
  394. var canvas, stage;
  395. var drawingCanvas;
  396. var oldPt;
  397. var oldMidPt;
  398. var color;
  399. var stroke;
  400. var index;
  401.  
  402. function init() {
  403. if (window.top != window) {
  404. document.getElementById("header").style.display = "none";
  405. }
  406. canvas = document.getElementById("sheet");
  407. index = 0;
  408.  
  409. //check to see if we are running in a browser with touch support
  410. stage = new createjs.Stage(canvas);
  411. stage.autoClear = false;
  412. stage.enableDOMEvents(true);
  413.  
  414. createjs.Touch.enable(stage);
  415. createjs.Ticker.setFPS(24);
  416.  
  417. drawingCanvas = new createjs.Shape();
  418.  
  419. stage.addEventListener("stagemousedown", handleMouseDown);
  420. stage.addEventListener("stagemouseup", handleMouseUp);
  421.  
  422. stage.addChild(drawingCanvas);
  423. stage.update();
  424. }
  425.  
  426. function stop() {}
  427.  
  428. function handleMouseDown(event) {
  429. color = "#ff0000";
  430. stroke = 5;
  431. oldPt = new createjs.Point(stage.mouseX, stage.mouseY);
  432. oldMidPt = oldPt;
  433. stage.addEventListener("stagemousemove" , handleMouseMove);
  434. }
  435.  
  436. function handleMouseMove(event) {
  437. var midPt = new createjs.Point(oldPt.x + stage.mouseX>>1, oldPt.y+stage.mouseY>>1);
  438.  
  439. drawingCanvas.graphics.clear().setStrokeStyle(stroke, 'round', 'round').beginStroke(color).moveTo(midPt.x, midPt.y).curveTo(oldPt.x, oldPt.y, oldMidPt.x, oldMidPt.y);
  440.  
  441. oldPt.x = stage.mouseX;
  442. oldPt.y = stage.mouseY;
  443.  
  444. oldMidPt.x = midPt.x;
  445. oldMidPt.y = midPt.y;
  446.  
  447. stage.update();
  448. }
  449.  
  450. function handleMouseUp(event) {
  451. stage.removeEventListener("stagemousemove" , handleMouseMove);
  452. }
  453.  
  454. </script>
  455. </head>
  456. <body onload="init();">
  457. <canvas id="sheet" width="400" height="400"></canvas>
  458. </body>
  459. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement