Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

70
LINES

< > BotCompany Repo | #1029696 // scribble.js

Document

1  
// Original fiddle: http://jsfiddle.net/R4V97/97/
2  
// Stefan's edit: jsfiddle.net/stefanreich/wskby4q5/
3  
4  
var canvas = document.getElementById("scribble"),
5  
    ctx = canvas.getContext("2d"),
6  
    painting = true,
7  
    lastX = 0,
8  
    lastY = 0,
9  
    mouseDown = false,
10  
    fadeInterval = 100,
11  
    fadeSpeed = 0.025,
12  
    strokeStyleDown = "rgba(255,255,255,1)",
13  
    strokeStyleUp = "rgba(255,255,255,0.025)",
14  
    sendStroke = null;
15  
16  
function drawStroke(x1, y1, x2, y2, mouseDown) {
17  
  ctx.strokeStyle = mouseDown ? strokeStyleDown : strokeStyleUp;
18  
  ctx.lineWidth = 3;
19  
  ctx.beginPath();
20  
  ctx.moveTo(x1, y1);
21  
  ctx.lineTo(x2, y2);
22  
  ctx.stroke();
23  
}
24  
  
25  
document.addEventListener('DOMContentLoaded', addMouseListeners, false);
26  
27  
function addMouseListeners() {
28  
  canvas.ontouchdown = function (e) {
29  
    console.log("touchdown " + e.pageX + "/" + e.pageY);
30  
    e.preventDefault();
31  
  };
32  
  
33  
  canvas.onmousedown = function (e) {
34  
    console.log("mousedown");
35  
      lastX = e.pageX /*- this.offsetLeft*/;
36  
      lastY = e.pageY /*- this.offsetTop*/;
37  
      mouseDown = true;
38  
  };
39  
  
40  
  canvas.onmouseup = function (e) {
41  
      mouseDown = false;
42  
  };
43  
  
44  
  canvas.onmouseout = function (e) {
45  
      mouseDown = false;
46  
  };
47  
  
48  
  canvas.onmousemove = function (e) {
49  
    //console.log("mousemove");
50  
    if (!painting) return;
51  
    mouseX = e.pageX/* - this.offsetLeft*/;
52  
    mouseY = e.pageY/* - this.offsetTop*/;
53  
    drawStroke(lastX, lastY, mouseX, mouseY, mouseDown);
54  
    if (sendStroke)
55  
      sendStroke({x1: lastX, y1: lastY, x2: mouseX, y2: mouseY, mouseDown: mouseDown});
56  
    lastX = mouseX;
57  
    lastY = mouseY;
58  
  };
59  
  
60  
  console.log("Registered mouse listeners on canvas");
61  
  
62  
  function fadeOut() {
63  
    var r = fadeSpeed * (1 + Math.random()*0.3);
64  
    ctx.fillStyle = "rgba(60,30,50,"+r+")";
65  
    ctx.fillRect(0, 0, canvas.width, canvas.height);
66  
    setTimeout(fadeOut, fadeInterval);
67  
  }
68  
  
69  
  fadeOut();
70  
}

download  show line numbers   

Travelled to 6 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1029696
Snippet name: scribble.js
Eternal ID of this version: #1029696/23
Text MD5: 671ee98950e235be5f4ccc8dc037d72c
Author: stefan
Category:
Type: Document
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-08-24 23:13:24
Source code size: 1958 bytes / 70 lines
Pitched / IR pitched: No / No
Views / Downloads: 170 / 512
Version history: 22 change(s)
Referenced in: [show references]