Last active
April 4, 2019 12:52
-
-
Save mimonelu/1ff8abd5346d5146ba339a045c980c89 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
(() => { | |
const retina = 2; | |
const lineWidth = 7; | |
const scratchCanvas = document.querySelector('.scratch-canvas'); | |
if (scratchCanvas) { | |
scratchCanvas.style['display'] = scratchCanvas.style['display'] === 'none' ? '' : 'none'; | |
} else { | |
const canvas = document.createElement('canvas'); | |
canvas.setAttribute('class', 'scratch-canvas'); | |
canvas.width = document.documentElement.scrollWidth * retina; | |
canvas.height = document.documentElement.scrollHeight * retina; | |
canvas.style.cssText = `position: absolute; top: 0; left: 0; width: ${canvas.width / retina}px; height: ${canvas.height / retina}px; z-index: 65535;`; | |
document.body.appendChild(canvas); | |
const context = canvas.getContext('2d'); | |
context.lineCap = 'round'; | |
context.lineJoin = 'round'; | |
let pointerIsDown = false; | |
let previousX = 0; | |
let previousY = 0; | |
canvas.addEventListener('pointerdown', e => { | |
pointerIsDown = true; | |
previousX = e.offsetX * retina; | |
previousY = e.offsetY * retina; | |
}); | |
canvas.addEventListener('pointerup', () => { | |
pointerIsDown = false; | |
}); | |
canvas.addEventListener('pointermove', e => { | |
if (!pointerIsDown) return; | |
context.beginPath(); | |
context.moveTo(previousX, previousY); | |
context.lineTo(e.offsetX * retina, e.offsetY * retina); | |
context.globalCompositeOperation = 'destination-over'; | |
context.lineWidth = lineWidth * retina; | |
context.strokeStyle = '#ffffff'; | |
context.stroke(); | |
context.globalCompositeOperation = 'source-over'; | |
context.lineWidth = lineWidth; | |
context.strokeStyle = '#ff0000'; | |
context.stroke(); | |
previousX = e.offsetX * retina; | |
previousY = e.offsetY * retina; | |
}); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
γγγ―γγΌγ―γ¬γγζ¬δ½γ―γγ‘γγ