Last active
December 5, 2022 21:17
-
-
Save kagklis/7acfb78adab3b51f8c4f89ba0e3f0293 to your computer and use it in GitHub Desktop.
This file contains 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
/// <reference lib="webworker" /> | |
importScripts( | |
'https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.9.1/chart.min.js' | |
); | |
declare const Chart: any; | |
let canvas: any; | |
let chart: any; | |
function firstDraw(data: any): void { | |
canvas = data.canvas; | |
plot(data); | |
} | |
function plot(data: any): void { | |
const ctx = canvas.getContext('2d'); | |
ctx.clearRect(0, 0, canvas.width, canvas.height); | |
chart = new Chart(canvas, data.chartData); | |
canvas.width = data.width; | |
canvas.height = data.height; | |
chart.resize(); | |
} | |
function redraw(data: any): void { | |
chart.destroy(); | |
plot(data); | |
} | |
const handlers = { | |
firstDraw, | |
redraw | |
}; | |
addEventListener('message', ({ data }) => { | |
const fn = handlers[data.type as keyof typeof handlers]; | |
if (!fn) { | |
throw new Error('No handler registered for type: ' + data.type); | |
} | |
fn(data); | |
postMessage({}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment