Skip to content

Commit

Permalink
basic touch support; Atrament can now be constructed from a Node, as …
Browse files Browse the repository at this point in the history
…well as a selector string
  • Loading branch information
jakubfiala committed Apr 3, 2016
1 parent 7f8ca07 commit 035c939
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
npm
uglifyjs
dev-demo
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ Enjoy!
```
+ in your JavaScript, call `atrament` passing in the selector string of your canvas:
```js
var sketcher = atrament('mySketcher');
var sketcher = atrament('#mySketcher');
//or use a more object-oriented style
var sketcher = new Atrament('mySketcher');
var sketcher = new Atrament('#mySketcher');
//you can also pass the canvas Node itself
var sketcher = new Atrament(document.querySelector('#mySketcher'));
```
+ you can also pass the width, height and default colour to the function:
```js
var sketcher = atrament('mySketcher', 500, 500, 'orange');
var sketcher = atrament('#mySketcher', 500, 500, 'orange');
```
+ that's it, happy drawing!

Expand Down
6 changes: 6 additions & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
margin-bottom: 1em;
}

@media (-webkit-min-device-pixel-ratio: 1.25), (min-resolution: 120dpi) {
form * {
font-size: 1.5em;
}
}

body {
background-image: url('http://www.publicdomainpictures.net/download-picture.php?adresar=50000&soubor=notebook-page.jpg');
font-family: sans-serif;
Expand Down
2 changes: 1 addition & 1 deletion dist/atrament.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 15 additions & 3 deletions src/atrament.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ class Atrament {
if (!document) throw new Error('no DOM found');

//get canvas element
this.canvas = document.querySelector(selector);
if (selector instanceof Node && selector.tagName === 'CANVAS') this.canvas = selector;
else if (typeof selector === 'string') this.canvas = document.querySelector(selector);
else throw new Error(`can\'t look for canvas based on \'${selector}\'`);
if (!this.canvas) throw new Error('canvas not found');

//set external canvas params
Expand All @@ -98,9 +100,14 @@ class Atrament {

//mousemove handler
let mouseMove = (mousePosition) => {
mousePosition.preventDefault();
//get position
let mx, my;
if (mousePosition.layerX || mousePosition.layerX == 0) { // Firefox
if (mousePosition.changedTouches) { // touchscreens
mx = mousePosition.changedTouches[0].pageX - mousePosition.target.offsetLeft;
my = mousePosition.changedTouches[0].pageY - mousePosition.target.offsetTop;
}
else if (mousePosition.layerX || mousePosition.layerX == 0) { // Firefox
mx = mousePosition.layerX;
my = mousePosition.layerY;
}
Expand All @@ -121,6 +128,7 @@ class Atrament {

//mousedown handler
let mouseDown = (mousePosition) => {
mousePosition.preventDefault();
//update position just in case
mouseMove(mousePosition);
//remember it
Expand All @@ -131,7 +139,9 @@ class Atrament {
this.context.beginPath();
this.context.moveTo(this.mouse.px, this.mouse.py);
}
let mouseUp = () => { this.mouse.down = false;
let mouseUp = (mousePosition) => {
mousePosition.preventDefault();
this.mouse.down = false;
//stop drawing
this.context.closePath();
}
Expand All @@ -140,6 +150,8 @@ class Atrament {
this.canvas.addEventListener('mousemove', mouseMove);
this.canvas.addEventListener('mousedown', mouseDown);
this.canvas.addEventListener('mouseup', mouseUp);
this.canvas.addEventListener('touchstart', mouseDown);
this.canvas.addEventListener('touchend', mouseUp);
this.canvas.addEventListener('touchmove', mouseMove);

//set internal canvas params
Expand Down

0 comments on commit 035c939

Please sign in to comment.