|
| 1 | +{{includeFile template="client/src/drawing/canvas.css.hbs" output="client/src/canvas.css"}} |
| 2 | + |
| 3 | +<script{{#if typescript}} lang="ts"{{/if}}> |
| 4 | +import './canvas.css'; |
| 5 | +import {onMount} from 'svelte'; |
| 6 | +import {getHlcFunctions} from 'tinybase'; |
| 7 | +import {canvasStore} from './canvasStore'; |
| 8 | +{{#if typescript}}import type {StrokeRow} from './canvasStore';{{/if}} |
| 9 | +import {settingsStore} from './settingsStore'; |
| 10 | +
|
| 11 | +const [getNextHlc] = getHlcFunctions(); |
| 12 | +
|
| 13 | +let canvas{{#if typescript}}: HTMLCanvasElement | undefined{{/if}}; |
| 14 | +let isDrawing = false; |
| 15 | +let currentStrokeId{{#if typescript}}: string | null{{/if}} = null; |
| 16 | +
|
| 17 | +const getPoint = (event{{#if typescript}}: MouseEvent | TouchEvent{{/if}}) => { |
| 18 | +if (!canvas) { |
| 19 | +return null; |
| 20 | +} |
| 21 | +
|
| 22 | +const rect = canvas.getBoundingClientRect(); |
| 23 | +const clientX = 'touches' in event ? event.touches[0]?.clientX : event.clientX; |
| 24 | +const clientY = 'touches' in event ? event.touches[0]?.clientY : event.clientY; |
| 25 | +
|
| 26 | +if (clientX == null || clientY == null) { |
| 27 | +return null; |
| 28 | +} |
| 29 | +
|
| 30 | +return { |
| 31 | +x: clientX - rect.left, |
| 32 | +y: clientY - rect.top, |
| 33 | +}; |
| 34 | +}; |
| 35 | +
|
| 36 | +const startStroke = (event{{#if typescript}}: MouseEvent | TouchEvent{{/if}}) => { |
| 37 | +isDrawing = true; |
| 38 | +currentStrokeId = getNextHlc(); |
| 39 | +
|
| 40 | +const point = getPoint(event); |
| 41 | +if (!point || !currentStrokeId) { |
| 42 | +return; |
| 43 | +} |
| 44 | +
|
| 45 | +const brushColor = |
| 46 | + {{#if typescript}}(settingsStore.getValue('brushColor') as string | undefined) ?? '#d81b60'{{else}}settingsStore.getValue('brushColor') ?? '#d81b60'{{/if}}; |
| 47 | +const brushSize = |
| 48 | + {{#if typescript}}(settingsStore.getValue('brushSize') as number | undefined) ?? 5{{else}}settingsStore.getValue('brushSize') ?? 5{{/if}}; |
| 49 | +
|
| 50 | +canvasStore.setRow('strokes', currentStrokeId, { |
| 51 | +color: brushColor, |
| 52 | +size: brushSize, |
| 53 | +points: JSON.stringify([point.x, point.y]), |
| 54 | +}); |
| 55 | +}; |
| 56 | +
|
| 57 | +const extendStroke = (event{{#if typescript}}: MouseEvent | TouchEvent{{/if}}) => { |
| 58 | +if (!isDrawing || !currentStrokeId) { |
| 59 | +return; |
| 60 | +} |
| 61 | +
|
| 62 | +const point = getPoint(event); |
| 63 | +if (!point) { |
| 64 | +return; |
| 65 | +} |
| 66 | +
|
| 67 | +const pointsArray = JSON.parse( |
| 68 | + {{#if typescript}}(canvasStore.getCell('strokes', currentStrokeId, 'points') as string | undefined) ?? '[]'{{else}}canvasStore.getCell('strokes', currentStrokeId, 'points') ?? '[]'{{/if}}, |
| 69 | +){{#if typescript}} as number[]{{/if}}; |
| 70 | +pointsArray.push(point.x, point.y); |
| 71 | +canvasStore.setCell('strokes', currentStrokeId, 'points', JSON.stringify(pointsArray)); |
| 72 | +}; |
| 73 | +
|
| 74 | +const endStroke = () => { |
| 75 | +isDrawing = false; |
| 76 | +currentStrokeId = null; |
| 77 | +}; |
| 78 | +
|
| 79 | +const handleTouchStart = (event{{#if typescript}}: TouchEvent{{/if}}) => { |
| 80 | +event.preventDefault(); |
| 81 | +startStroke(event); |
| 82 | +}; |
| 83 | +
|
| 84 | +const handleTouchMove = (event{{#if typescript}}: TouchEvent{{/if}}) => { |
| 85 | +event.preventDefault(); |
| 86 | +extendStroke(event); |
| 87 | +}; |
| 88 | +
|
| 89 | +onMount(() => { |
| 90 | +const draw = () => { |
| 91 | +if (!canvas) { |
| 92 | +return; |
| 93 | +} |
| 94 | +
|
| 95 | +const ctx = canvas.getContext('2d'); |
| 96 | +if (!ctx) { |
| 97 | +return; |
| 98 | +} |
| 99 | +
|
| 100 | +ctx.fillStyle = '#111'; |
| 101 | +ctx.fillRect(0, 0, canvas.width, canvas.height); |
| 102 | +
|
| 103 | +canvasStore.getSortedRowIds('strokes').forEach((id) => { |
| 104 | +const stroke = |
| 105 | + {{#if typescript}}canvasStore.getRow('strokes', id) as StrokeRow{{else}}canvasStore.getRow('strokes', id){{/if}}; |
| 106 | +if (!stroke?.points) { |
| 107 | +return; |
| 108 | +} |
| 109 | +
|
| 110 | +const pointsArray = JSON.parse(stroke.points){{#if typescript}} as number[]{{/if}}; |
| 111 | +if (pointsArray.length < 2) { |
| 112 | +return; |
| 113 | +} |
| 114 | +
|
| 115 | +ctx.strokeStyle = stroke.color; |
| 116 | +ctx.lineWidth = stroke.size * 2; |
| 117 | +ctx.lineCap = 'round'; |
| 118 | +ctx.lineJoin = 'round'; |
| 119 | +ctx.beginPath(); |
| 120 | +ctx.moveTo(pointsArray[0], pointsArray[1]); |
| 121 | +for (let i = 2; i < pointsArray.length; i += 2) { |
| 122 | +ctx.lineTo(pointsArray[i], pointsArray[i + 1]); |
| 123 | +} |
| 124 | +ctx.stroke(); |
| 125 | +}); |
| 126 | +}; |
| 127 | +
|
| 128 | +canvasStore.addTablesListener(draw); |
| 129 | +draw(); |
| 130 | +}); |
| 131 | +</script> |
| 132 | +
|
| 133 | +<canvas |
| 134 | + bind:this={canvas} |
| 135 | + id="drawingCanvas" |
| 136 | + width="600" |
| 137 | + height="400" |
| 138 | + onmousedown={startStroke} |
| 139 | + onmousemove={extendStroke} |
| 140 | + onmouseup={endStroke} |
| 141 | + onmouseleave={endStroke} |
| 142 | + ontouchstart={handleTouchStart} |
| 143 | + ontouchmove={handleTouchMove} |
| 144 | + ontouchend={endStroke} |
| 145 | +></canvas> |
0 commit comments