Created
September 24, 2019 13:09
-
-
Save azl397985856/ca337adec10e9f3cbe35bc98114dbf02 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
import { render } from 'react-dom'; | |
// Get the position of the mouse relative to the canvas | |
function getMousePos(canvasDom, mouseEvent) { | |
var rect = canvasDom.getBoundingClientRect(); | |
return { | |
x: mouseEvent.clientX - rect.left, | |
y: mouseEvent.clientY - rect.top | |
}; | |
} | |
// Get the position of a touch relative to the canvas | |
function getTouchPos(canvasDom, touchEvent) { | |
var rect = canvasDom.getBoundingClientRect(); | |
return { | |
x: touchEvent.touches[0].clientX - rect.left, | |
y: touchEvent.touches[0].clientY - rect.top | |
}; | |
} | |
class App extends Component { | |
constructor() { | |
super(); | |
} | |
componentDidMount() { | |
const canvas = document.getElementById("ctx"); | |
var ctx = canvas.getContext("2d"); | |
ctx.strokeStyle = "red"; | |
ctx.lineWith = 2; | |
var drawing = false; | |
var mousePos = { x: 0, y: 0 }; | |
var lastPos = mousePos; | |
canvas.addEventListener("mousedown", function (e) { | |
drawing = true; | |
lastPos = getMousePos(canvas, e); | |
}, false); | |
canvas.addEventListener("mouseup", function (e) { | |
drawing = false; | |
}, false); | |
canvas.addEventListener("mousemove", function (e) { | |
mousePos = getMousePos(canvas, e); | |
console.log(lastPos) | |
}, false); | |
function renderCanvas() { | |
if (drawing) { | |
ctx.moveTo(lastPos.x, lastPos.y); | |
ctx.lineTo(mousePos.x, mousePos.y); | |
ctx.stroke(); | |
lastPos = mousePos; | |
} | |
} | |
// Allow for animation | |
(function drawLoop() { | |
window.requestAnimationFrame(drawLoop); | |
renderCanvas(); | |
})(); | |
// Set up touch events for mobile, etc | |
canvas.addEventListener("touchstart", function (e) { | |
mousePos = getTouchPos(canvas, e); | |
var touch = e.touches[0]; | |
var mouseEvent = new MouseEvent("mousedown", { | |
clientX: touch.clientX, | |
clientY: touch.clientY | |
}); | |
canvas.dispatchEvent(mouseEvent); | |
}, false); | |
canvas.addEventListener("touchend", function (e) { | |
var mouseEvent = new MouseEvent("mouseup", {}); | |
canvas.dispatchEvent(mouseEvent); | |
}, false); | |
canvas.addEventListener("touchmove", function (e) { | |
var touch = e.touches[0]; | |
var mouseEvent = new MouseEvent("mousemove", { | |
clientX: touch.clientX, | |
clientY: touch.clientY | |
}); | |
canvas.dispatchEvent(mouseEvent); | |
}, false); | |
// Prevent scrolling when touching the canvas | |
document.body.addEventListener("touchstart", function (e) { | |
if (e.target == canvas) { | |
e.preventDefault(); | |
} | |
}, false); | |
document.body.addEventListener("touchend", function (e) { | |
if (e.target == canvas) { | |
e.preventDefault(); | |
} | |
}, false); | |
document.body.addEventListener("touchmove", function (e) { | |
if (e.target == canvas) { | |
e.preventDefault(); | |
} | |
}, false); | |
} | |
render() { | |
return ( | |
<canvas id="ctx" width="300px" height="300px"> | |
</canvas> | |
); | |
} | |
} | |
render(<App />, document.getElementById('root')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment