Created
March 3, 2018 14:05
-
-
Save lonekorean/d529d9739bb67e11e5e3880f4498ff7b to your computer and use it in GitHub Desktop.
Paint worklet to create jagged edge
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
class JaggedEdgePainter { | |
static get inputProperties() { | |
return ['--tooth-width', '--tooth-height']; | |
} | |
paint(ctx, size, props) { | |
let toothWidth = props.get('--tooth-width').value; | |
let toothHeight = props.get('--tooth-height').value; | |
// lots of math to ensure teeth are collectively centered | |
let spaceBeforeCenterTooth = (size.width - toothWidth) / 2; | |
let teethBeforeCenterTooth = Math.ceil(spaceBeforeCenterTooth / toothWidth); | |
let totalTeeth = teethBeforeCenterTooth * 2 + 1; | |
let startX = spaceBeforeCenterTooth - teethBeforeCenterTooth * toothWidth; | |
// start drawing teeth from left | |
ctx.beginPath(); | |
ctx.moveTo(startX, toothHeight); | |
// draw the top zig-zag for all the teeth | |
for (let i = 0; i < totalTeeth; i++) { | |
let x = startX + toothWidth * i; | |
ctx.lineTo(x + toothWidth / 2, 0); | |
ctx.lineTo(x + toothWidth, toothHeight); | |
} | |
// surround the area below the teeth and fill it all in | |
ctx.lineTo(size.width, size.height); | |
ctx.lineTo(0, size.height); | |
ctx.closePath(); | |
ctx.fill(); | |
} | |
} | |
registerPaint('jagged-edge', JaggedEdgePainter); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment