{"id":336871,"date":"2022-08-10T21:00:06","date_gmt":"2022-08-10T21:00:06","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=336871"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=336871","title":{"rendered":"<span>\u041a\u0430\u043a \u0441\u043e\u043a\u0440\u0430\u0442\u0438\u0442\u044c \u043a\u043e\u0434 Canvas API \u0432 Svelte<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/webt\/tq\/gh\/tc\/tqghtcr4w2l9cpcatosz4hmpmxq.png\" width=\"auto\" height=\"auto\" data-src=\"https:\/\/habrastorage.org\/webt\/tq\/gh\/tc\/tqghtcr4w2l9cpcatosz4hmpmxq.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0420\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u0438\u0437 \u043a\u043e\u043d\u0441\u0430\u043b\u0442\u0438\u043d\u0433\u043e\u0432\u043e\u0439 \u043a\u043e\u043c\u043f\u0430\u043d\u0438\u0438 \u0432 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438 This Dot Labs \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442, \u043a\u0430\u043a \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c canvas \u0432 Svelte \u0438 \u043a\u0430\u043a \u043f\u0440\u0435\u0432\u0440\u0430\u0442\u0438\u0442\u044c \u043c\u043d\u043e\u0433\u043e\u0441\u043b\u043e\u0432\u043d\u044b\u0439 API Canvas \u0432 \u043a\u0440\u0430\u0442\u043a\u0438\u0439, \u0431\u043e\u043b\u0435\u0435 \u0434\u0435\u043a\u043b\u0430\u0440\u0430\u0442\u0438\u0432\u043d\u044b\u0439. \u041f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0441\u0442\u0438 \u2014 \u043a \u0441\u0442\u0430\u0440\u0442\u0443 \u043d\u0430\u0448\u0435\u0433\u043e <a href=\"https:\/\/skillfactory.ru\/frontend-razrabotchik?utm_source=habr&amp;utm_medium=habr&amp;utm_campaign=article&amp;utm_content=coding_fr_100822&amp;utm_term=lead\"><u>\u043a\u0443\u0440\u0441\u0430 \u043f\u043e \u0444\u0440\u043e\u043d\u0442\u0435\u043d\u0434\u0443<\/u><\/a>.<\/p>\n<hr\/>\n<p>\u042d\u043b\u0435\u043c\u0435\u043d\u0442 <code>&lt;canvas><\/code> \u0438 Canvas API \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0440\u0438\u0441\u043e\u0432\u0430\u0442\u044c \u043d\u0430 JavaScript, \u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e Svelte \u0435\u0433\u043e \u0438\u043c\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u044b\u0439 API \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u0434\u0435\u043a\u043b\u0430\u0440\u0430\u0442\u0438\u0432\u043d\u044b\u0439. \u042d\u0442\u043e \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u043e\u0442 \u0432\u0430\u0441 \u0437\u043d\u0430\u043d\u0438\u044f Renderless-\u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432 \u2014 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435 \u043e\u0442\u0440\u0438\u0441\u043e\u0432\u044b\u0432\u0430\u044e\u0442\u0441\u044f.<\/p>\n<h3>Renderless<\/h3>\n<p>\u0412\u0441\u0435 \u0440\u0430\u0437\u0434\u0435\u043b\u044b \u0444\u0430\u0439\u043b\u0430 <code>.svelte<\/code>, \u0432\u043a\u043b\u044e\u0447\u0430\u044f \u0448\u0430\u0431\u043b\u043e\u043d, \u043d\u0435\u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u044b. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0435 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0435\u0442\u0441\u044f, \u043d\u043e \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043b\u043e\u0433\u0438\u043a\u0443 \u0432 \u0442\u0435\u0433\u0435 <code>&lt;script><\/code>.<\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043d\u043e\u0432\u044b\u0439 \u043f\u0440\u043e\u0435\u043a\u0442 Svelte \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e Vite:<\/p>\n<pre><code class=\"bash\">npm init vite   Project name: canvas-svelte  Select a framework: \u203a svelte  Select a variant: \u203a svelte-ts  cd canvas-svelte npm i<\/code><\/pre>\n<p>\u0418 \u043d\u043e\u0432\u044b\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u2014 <code>Renderless<\/code>:<\/p>\n<pre><code class=\"xml\">&lt;!-- src\/lib\/Renderless.svelte --> &lt;script>     console.log(\"No template\"); &lt;\/script><\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430 \u0432\u044b\u0432\u0435\u0434\u0435\u043c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043f\u0435\u0440\u0435\u043f\u0438\u0448\u0435\u043c \u0442\u043e\u0447\u043a\u0443 \u0432\u0445\u043e\u0434\u0430 <code>App<\/code>:<\/p>\n<pre><code class=\"typescript\">\/\/ src\/main.ts \/\/ import App from '.\/App.svelte' import Renderless from '.\/lib\/Renderless.svelte'  const app = new Renderless({   target: document.getElementById('app') })  export default app<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0441\u0435\u0440\u0432\u0435\u0440, \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u0435\u043c \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430 \u2014 \u0438 \u0432\u0438\u0434\u0438\u043c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/722\/42b\/0f0\/72242b0f05268748749539d7e035da89.png\" width=\"1008\" height=\"508\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/722\/42b\/0f0\/72242b0f05268748749539d7e035da89.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0420\u0430\u0431\u043e\u0442\u0430\u0435\u0442.<\/p>\n<blockquote>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u043c\u0435\u0442\u043e\u0434\u044b \u0436\u0438\u0437\u043d\u0435\u043d\u043d\u043e\u0433\u043e \u0446\u0438\u043a\u043b\u0430 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430 \u043f\u043e-\u043f\u0440\u0435\u0436\u043d\u0435\u043c\u0443 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b, \u0442\u043e \u0435\u0441\u0442\u044c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u0432\u0435\u0434\u0451\u0442 \u0441\u0435\u0431\u044f \u043a\u0430\u043a \u043e\u0431\u044b\u0447\u043d\u044b\u0439, \u0434\u0430\u0436\u0435 \u043a\u043e\u0433\u0434\u0430 \u0443 \u043d\u0435\u0433\u043e \u043d\u0435\u0442 \u0448\u0430\u0431\u043b\u043e\u043d\u0430.<\/p>\n<\/blockquote>\n<p>\u041f\u0440\u043e\u0432\u0435\u0440\u0438\u043c \u044d\u0442\u043e:<\/p>\n<pre><code class=\"typescript\">&lt;!-- src\/lib\/Renderless.svelte --> &lt;script>     import { onMount } from \"svelte\";     console.log(\"No template\");     onMount(() => {         console.log(\"Component mounted\");     }); &lt;\/script><\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u043c\u043e\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f Renderless \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0435\u0442 \u0432\u0442\u043e\u0440\u043e\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435, \u043e\u0431\u0430 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u0432\u044b\u0432\u043e\u0434\u044f\u0442\u0441\u044f \u0432 \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/d6f\/934\/0a2\/d6f9340a2e23ee1abf19ed0fcbc8627f.png\" width=\"1020\" height=\"552\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/d6f\/934\/0a2\/d6f9340a2e23ee1abf19ed0fcbc8627f.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e Renderless \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u0430\u043a \u043b\u044e\u0431\u043e\u0439 \u0434\u0440\u0443\u0433\u043e\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 Svelte. \u0412\u0435\u0440\u043d\u0451\u043c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f <code>main.ts<\/code> \u0438 &#171;\u043e\u0442\u0440\u0438\u0441\u0443\u0435\u043c&#187; \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u0432\u043d\u0443\u0442\u0440\u0438 <code>App<\/code>:<\/p>\n<pre><code class=\"typescript\">\/\/ src\/main.ts import App from '.\/App.svelte'  const app = new App({   target: document.getElementById('app') })  export default app<\/code><\/pre>\n<pre><code class=\"xml\">&lt;!-- src\/App.svelte --> &lt;script lang=\"ts\">   import { onMount } from \"svelte\";    import Renderless from \".\/lib\/Renderless.svelte\";   console.log(\"App: initialized\");   onMount(() => {     console.log(\"App: mounted\");   }); &lt;\/script>  &lt;main>   &lt;Renderless \/> &lt;\/main><\/code><\/pre>\n<p>\u041f\u0435\u0440\u0435\u043f\u0438\u0448\u0435\u043c Renderless, \u0447\u0442\u043e\u0431\u044b \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0430\u0436\u043d\u044b\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f:<\/p>\n<pre><code class=\"xml\">&lt;!-- src\/lib\/Renderless.svelte --> &lt;script>     import { onMount } from \"svelte\";     console.log(\"Renderless: initialized\");     onMount(() => {         console.log(\"Renderless: mounted\");     }); &lt;\/script><\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/59e\/a6e\/fac\/59ea6efac42dca3de85ef5b518d112a9.png\" width=\"1024\" height=\"658\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/59e\/a6e\/fac\/59ea6efac42dca3de85ef5b518d112a9.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u041f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432 \u0431\u0435\u0437 \u043e\u0442\u0440\u0438\u0441\u043e\u0432\u043a\u0438 \u0438 Canvas \u0432\u0430\u0436\u043d\u043e \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0438 \u043c\u043e\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432.<\/p>\n<p>\u0415\u0449\u0451 \u043e\u0434\u0438\u043d \u0441\u043f\u043e\u0441\u043e\u0431 \u043c\u043e\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u2014 \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0435\u0433\u043e \u043a\u0430\u043a \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0434\u0440\u0443\u0433\u043e\u0433\u043e \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430. \u0422\u0430\u043a\u0430\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0435\u043a\u0446\u0438\u0435\u0439 \u043a\u043e\u043d\u0442\u0435\u043d\u0442\u0430. \u042d\u0442\u0443 \u043f\u0440\u043e\u0435\u043a\u0446\u0438\u044e \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>slot<\/code>.<\/p>\n<p>\u041d\u0430\u043f\u0438\u0448\u0435\u043c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 <code>Container<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u0440\u0438\u0441\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0435 \u0432 \u0441\u043b\u043e\u0442 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b:<\/p>\n<pre><code class=\"typescript\">&lt;!-- src\/lib\/Container.svelte --> &lt;script>     import { onMount } from \"svelte\";     console.log(\"Container: initialized\");     onMount(() => {         console.log(\"Container: mounted\");     }); &lt;\/script>  &lt;h1>The container of things&lt;\/h1> &lt;slot \/> &lt;p>invisible things&lt;\/p><\/code><\/pre>\n<p>\u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>prop<\/code> \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0432 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 <code>Renderless<\/code> \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440:<\/p>\n<pre><code class=\"xml\">&lt;!-- src\/lib\/Renderless.svelte --> &lt;script lang=\"ts\">     import { onMount } from \"svelte\";     export let id:string = \"NoId\"     console.log(`Renderless ${id}: initialized`);     onMount(() => {         console.log(`Renderless ${id}: mounted`);     }); &lt;\/script> <\/code><\/pre>\n<p>\u041f\u0435\u0440\u0435\u043f\u0438\u0448\u0435\u043c <code>App<\/code> \u0434\u043b\u044f \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u0430, \u0437\u0430\u0442\u0435\u043c \u043f\u0435\u0440\u0435\u0434\u0430\u0434\u0438\u043c \u0432 <code>App<\/code> \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u043e\u0432 <code>Renderless<\/code>:<\/p>\n<pre><code class=\"typescript\">&lt;!-- src\/App.svelte --> &lt;script lang=\"ts\">   import { onMount } from \"svelte\";   import Container from \".\/lib\/Container.svelte\";    import Renderless from \".\/lib\/Renderless.svelte\";   console.log(\"App: initialized\");   onMount(() => {     console.log(\"App: mounted\");   }); &lt;\/script>  &lt;main>   &lt;Container>     &lt;Renderless id=\"Foo\"\/>     &lt;Renderless id=\"Bar\"\/>     &lt;Renderless id=\"Baz\"\/>   &lt;\/Container> &lt;\/main><\/code><\/pre>\n<p>\u041d\u0438\u0436\u0435 \u0432\u0438\u0434\u043d\u043e \u0438 <code>Container<\/code>, \u0438 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b \u0431\u0435\u0437 \u043e\u0442\u0440\u0438\u0441\u043e\u0432\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0440\u0438 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0438 \u043c\u043e\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u043f\u0438\u0448\u0443\u0442 \u0432 \u043b\u043e\u0433:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/8da\/37a\/932\/8da37a932b7d0617ce04b59cdcb602a2.png\" width=\"1304\" height=\"974\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/8da\/37a\/932\/8da37a932b7d0617ce04b59cdcb602a2.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430\u043c\u0438 \u0431\u0435\u0437 \u043e\u0442\u0440\u0438\u0441\u043e\u0432\u043a\u0438 \u0432 \u0441\u043e\u0447\u0435\u0442\u0430\u043d\u0438\u0438 \u0441 <code>&lt;canvas><\/code>.<\/p>\n<h3>HTML canvas \u0438 Canvas API<\/h3>\n<p>\u042d\u043b\u0435\u043c\u0435\u043d\u0442 <code>canvas<\/code> \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043d\u0438\u043a\u0430\u043a\u0438\u0445 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u043a\u0440\u043e\u043c\u0435 \u0440\u0435\u0437\u0435\u0440\u0432\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0434\u043b\u044f \u043e\u0442\u0440\u0438\u0441\u043e\u0432\u043a\u0438. \u0412\u0441\u0451, \u0447\u0442\u043e \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u0432 canvas, \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043e \u043d\u0430 \u0438\u043c\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u043e\u043c API.<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043d\u043e\u0432\u044b\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 Canvas \u0438 \u043e\u0442\u0440\u0438\u0441\u0443\u0435\u043c canvas:<\/p>\n<pre><code class=\"xml\">&lt;!-- src\/lib\/Canvas.svelte --> &lt;script>     import { onMount } from \"svelte\";      console.log(\"Canvas: initialized\");     onMount(() => {         console.log(\"Canvas: mounted\");     }); &lt;\/script>  &lt;canvas \/><\/code><\/pre>\n<p>\u041e\u0431\u043d\u043e\u0432\u0438\u043c <code>App<\/code>, \u0447\u0442\u043e\u0431\u044b \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f <code>Canvas<\/code>: <\/p>\n<pre><code class=\"xml\">&lt;!-- src\/App.svelte --> &lt;script lang=\"ts\">   import { onMount } from \"svelte\";   import Canvas from \".\/lib\/Canvas.svelte\";    console.log(\"App: initialized\");   onMount(() => {     console.log(\"App: mounted\");   }); &lt;\/script>  &lt;main>  &lt;Canvas \/> &lt;\/main><\/code><\/pre>\n<p>\u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u043e\u0442\u043a\u0440\u043e\u0435\u043c \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/299\/b3a\/1fb\/299b3a1fbda47667c53e27d8f0966ce6.png\" width=\"1032\" height=\"450\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/299\/b3a\/1fb\/299b3a1fbda47667c53e27d8f0966ce6.png\"\/><figcaption><\/figcaption><\/figure>\n<h4>\u041e\u0442\u0440\u0438\u0441\u043e\u0432\u043a\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432\u043d\u0443\u0442\u0440\u0438 canvas<\/h4>\n<p>\u041a\u0430\u043a \u0443\u0436\u0435 \u0433\u043e\u0432\u043e\u0440\u0438\u043b\u043e\u0441\u044c, \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u0440\u044f\u043c\u043e \u0432 canvas \u043d\u0435\u043b\u044c\u0437\u044f. \u0427\u0442\u043e\u0431\u044b \u0440\u0438\u0441\u043e\u0432\u0430\u0442\u044c, \u043d\u0443\u0436\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 API.<\/p>\n<p>\u0421\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0447\u0435\u0440\u0435\u0437 <code>bind:this<\/code>. \u0412\u0430\u0436\u043d\u043e \u043f\u043e\u043d\u0438\u043c\u0430\u0442\u044c, \u0447\u0442\u043e \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 API \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d, \u0442\u043e \u0435\u0441\u0442\u044c \u0440\u0438\u0441\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u0438\u0434\u0451\u0442\u0441\u044f \u043f\u043e\u0441\u043b\u0435 \u043c\u043e\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430:<\/p>\n<pre><code class=\"xml\">&lt;script lang=\"ts\">     import { onMount } from \"svelte\";     let canvasElement: HTMLCanvasElement     console.log(\"1\", canvasElement) \/\/ undefined!!!     console.log(\"Canvas: initialized\");     onMount(() => {         console.log(\"2\", canvasElement) \/\/ OK!!!         console.log(\"Canvas: mounted\");     }); &lt;\/script>  &lt;canvas bind:this={canvasElement}\/><\/code><\/pre>\n<p>\u041d\u0430\u0440\u0438\u0441\u0443\u0435\u043c \u043b\u0438\u043d\u0438\u044e. \u0414\u043b\u044f \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u043e\u0441\u0442\u0438 \u044f \u0443\u0431\u0440\u0430\u043b \u0432\u0441\u0451 \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435:<\/p>\n<pre><code class=\"xml\">&lt;script lang=\"ts\">     import { onMount } from \"svelte\";     let canvasElement: HTMLCanvasElement     onMount(() => {         \/\/ get canvas context         let ctx = canvasElement.getContext(\"2d\")          \/\/ draw line         ctx.beginPath();         ctx.moveTo(10, 20); \/\/ line will start here         ctx.lineTo(150, 100); \/\/ line ends here         ctx.stroke(); \/\/ draw it     }); &lt;\/script>  &lt;canvas bind:this={canvasElement}\/><\/code><\/pre>\n<p>\u0427\u0442\u043e\u0431\u044b \u0440\u0438\u0441\u043e\u0432\u0430\u0442\u044c, canvas \u043d\u0443\u0436\u0435\u043d \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442, \u0442\u0430\u043a \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0441\u043b\u0435 \u043c\u043e\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/1c4\/cee\/2c8\/1c4cee2c86c697a520bba095b1f0addc.png\" width=\"912\" height=\"394\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/1c4\/cee\/2c8\/1c4cee2c86c697a520bba095b1f0addc.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0415\u0441\u043b\u0438 \u0437\u0430\u0445\u043e\u0447\u0435\u0442\u0441\u044f \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432\u0442\u043e\u0440\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443, \u043f\u0440\u0438\u0434\u0451\u0442\u0441\u044f \u0434\u043e\u043f\u0438\u0441\u0430\u0442\u044c \u0438 \u043d\u043e\u0432\u044b\u0439 \u0431\u043b\u043e\u043a \u043a\u043e\u0434\u0430:<\/p>\n<pre><code class=\"xml\">&lt;script lang=\"ts\">     import { onMount } from \"svelte\";     let canvasElement: HTMLCanvasElement     onMount(() => {         \/\/ get canvas context         let ctx = canvasElement.getContext(\"2d\")          \/\/ draw first line         ctx.beginPath();         ctx.moveTo(10, 20); \/\/ line will start here         ctx.lineTo(150, 100); \/\/ line ends here         ctx.stroke(); \/\/ draw it         \/\/ draw second line         ctx.beginPath();         ctx.moveTo(10, 40); \/\/ line will start here         ctx.lineTo(150, 120); \/\/ line ends here         ctx.stroke(); \/\/ draw it     }); &lt;\/script><\/code><\/pre>\n<p>\u041c\u044b \u0440\u0438\u0441\u0443\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u044b\u0435 \u0444\u0438\u0433\u0443\u0440\u044b \u2014 \u0430 \u043a\u043e\u0434\u0430 \u0432 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0435 \u0432\u0441\u0451 \u0431\u043e\u043b\u044c\u0448\u0435 \u0438 \u0431\u043e\u043b\u044c\u0448\u0435. \u041c\u043e\u0436\u043d\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0441\u043e\u043a\u0440\u0430\u0449\u0430\u044e\u0449\u0438\u0435 \u043a\u043e\u0434 \u043b\u0438\u043d\u0438\u0439:<\/p>\n<pre><code class=\"typescript\">&lt;script lang=\"ts\">     import { onMount } from \"svelte\";     let canvasElement: HTMLCanvasElement;     onMount(() => {         \/\/ get canvas context         let ctx = canvasElement.getContext(\"2d\");          \/\/ draw first line         drawLine(ctx, [10, 20], [150, 100]);          \/\/ draw second line         drawLine(ctx, [10, 40], [150, 120]);     });      type Point = [number, number];     function drawLine(ctx: CanvasRenderingContext2D, start: Point, end: Point) {         ctx.beginPath();         ctx.moveTo(...start); \/\/ line will start here         ctx.lineTo(...end); \/\/ line ends here         ctx.stroke(); \/\/ draw it     } &lt;\/script>  &lt;canvas bind:this={canvasElement} \/><\/code><\/pre>\n<p>\u0427\u0438\u0442\u0430\u0442\u044c \u043a\u043e\u0434 \u043b\u0435\u0433\u0447\u0435, \u043d\u043e \u0432\u0441\u044f \u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u043f\u043e-\u043f\u0440\u0435\u0436\u043d\u0435\u043c\u0443 \u0434\u0435\u043b\u0435\u0433\u0438\u0440\u0443\u0435\u0442\u0441\u044f <code>Canvas<\/code>, \u0430 \u044d\u0442\u043e \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442 \u043a \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430. \u0418\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043f\u043e\u043c\u043e\u0433\u0443\u0442 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b \u0431\u0435\u0437 \u043e\u0442\u0440\u0438\u0441\u043e\u0432\u043a\u0438 \u0438 API Context.<\/p>\n<p>\u0418 \u0432\u043e\u0442 \u0447\u0442\u043e \u043d\u0430\u043c \u0443\u0436\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e:<\/p>\n<ul>\n<li>\n<p>\u0414\u043b\u044f \u0440\u0438\u0441\u043e\u0432\u0430\u043d\u0438\u044f \u043d\u0430\u043c \u043d\u0443\u0436\u0435\u043d Canvas.<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043b\u0435 \u043c\u043e\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430.<\/p>\n<\/li>\n<li>\n<p>\u0414\u043e\u0447\u0435\u0440\u043d\u0438\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b \u043c\u043e\u043d\u0442\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u043f\u0435\u0440\u0435\u0434 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u043c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u043c.<\/p>\n<\/li>\n<li>\n<p>\u0420\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u043f\u0435\u0440\u0435\u0434 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u043c\u0438 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430\u043c\u0438.<\/p>\n<\/li>\n<li>\n<p>\u0414\u043e\u0447\u0435\u0440\u043d\u0438\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u0438 \u043c\u043e\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438.<\/p>\n<\/li>\n<\/ul>\n<p>\u041d\u0430\u0448 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u043d\u0443\u0436\u043d\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c \u043d\u0430 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432. \u0417\u0434\u0435\u0441\u044c \u0445\u043e\u0447\u0435\u0442\u0441\u044f, \u0447\u0442\u043e\u0431\u044b <code>Line<\/code> \u0440\u0438\u0441\u043e\u0432\u0430\u043b \u0441\u0430\u043c \u0441\u0435\u0431\u044f.<\/p>\n<p><code>Canvas<\/code> \u0438 <code>Line<\/code> \u0441\u0432\u044f\u0437\u0430\u043d\u044b. <code>Line<\/code> \u043d\u0435\u043b\u044c\u0437\u044f \u043e\u0442\u0440\u0438\u0441\u043e\u0432\u0430\u0442\u044c \u0431\u0435\u0437 Canvas, \u0438 \u0435\u043c\u0443 \u043d\u0443\u0436\u0435\u043d \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 <code>canvas<\/code>. \u041d\u043e \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u043d\u0435\u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d, \u043a\u043e\u0433\u0434\u0430 \u043c\u043e\u043d\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442, \u0432\u0435\u0434\u044c <code>Line<\/code> \u043c\u043e\u043d\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043f\u0435\u0440\u0435\u0434 <code>Canvas<\/code>. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u043e\u0434\u0445\u043e\u0434 \u043d\u0443\u0436\u0435\u043d \u0434\u0440\u0443\u0433\u043e\u0439.<\/p>\n<p>\u0412\u043c\u0435\u0441\u0442\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u0434\u043b\u044f \u043e\u0442\u0440\u0438\u0441\u043e\u0432\u043a\u0438 \u0441\u0430\u043c\u043e\u0433\u043e \u0441\u0435\u0431\u044f \u0441\u043e\u043e\u0431\u0449\u0438\u043c \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u043c\u0443 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0443, \u0447\u0442\u043e \u0440\u0438\u0441\u043e\u0432\u0430\u0442\u044c \u043d\u0443\u0436\u043d\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442. <code>Canvas<\/code> \u0438 <code>Line<\/code> \u0441\u043e\u0435\u0434\u0438\u043d\u0438\u043c \u0447\u0435\u0440\u0435\u0437 <code>Context<\/code>.<\/p>\n<p><code>Context<\/code> \u2014 \u044d\u0442\u043e \u0441\u043f\u043e\u0441\u043e\u0431 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u0434\u0432\u0443\u0445 \u0438 \u0431\u043e\u043b\u0435\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432. \u0415\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0438\u043b\u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438, \u0430 \u044d\u0442\u043e \u043d\u0430\u043c \u0438 \u043d\u0443\u0436\u043d\u043e: <code>Canvas<\/code> \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043f\u0435\u0440\u0435\u0434 <code>Line<\/code>.<\/p>\n<p>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u0435\u0440\u0435\u043d\u0435\u0441\u0451\u043c \u043e\u0442\u0440\u0438\u0441\u043e\u0432\u043a\u0443 \u043b\u0438\u043d\u0438\u0438 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442, \u0430 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0442\u0438\u043f\u044b \u2014 \u0432 \u0438\u0445 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0444\u0430\u0439\u043b, \u0447\u0442\u043e\u0431\u044b \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0438\u0445 \u043e\u0431\u0449\u0438\u043c\u0438 \u0434\u043b\u044f \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432:<\/p>\n<pre><code class=\"typescript\">\/\/ src\/types.ts export type Point = [number, number]; export type DrawFn = (ctx: CanvasRenderingContext2D) => void; export type CanvasContext = {   addDrawFn: (fn: DrawFn) => void;   removeDrawFn: (fn: DrawFn) => void; };<\/code><\/pre>\n<pre><code class=\"xml\">&lt;!-- src\/lib\/Line.svelte --> &lt;script lang=\"ts\">     import type { Point } from \".\/types\";      export let start: Point;     export let end: Point;      function draw(ctx: CanvasRenderingContext2D) {         ctx.beginPath();         ctx.moveTo(...start);         ctx.lineTo(...end);         ctx.stroke();     } &lt;\/script><\/code><\/pre>\n<p>\u042d\u0442\u043e \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u0445\u043e\u0436\u0435 \u043d\u0430 \u0442\u043e, \u0447\u0442\u043e \u0431\u044b\u043b\u043e \u0432 <code>Canvas<\/code>, \u043d\u043e \u0430\u0431\u0441\u0442\u0440\u0430\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u043e \u0434\u043e \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430. \u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0443\u0436\u043d\u043e \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u043c\u043c\u0443\u043d\u0438\u043a\u0430\u0446\u0438\u044e <code>Canvas<\/code> \u0438 <code>Line<\/code>.<\/p>\n<p><code>Canvas<\/code> \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043a\u0430\u043a \u043e\u0440\u043a\u0435\u0441\u0442\u0440\u0430\u0442\u043e\u0440 \u0432\u0441\u0435\u0439 \u043e\u0442\u0440\u0438\u0441\u043e\u0432\u043a\u0438. \u041e\u043d \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u0442 \u0432\u0441\u0435 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b, \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043e\u0442\u0440\u0438\u0441\u043e\u0432\u043a\u0438 \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u043e\u0442\u0440\u0438\u0441\u043e\u0432\u043a\u0443, \u043a\u043e\u0433\u0434\u0430 \u044d\u0442\u043e \u043d\u0443\u0436\u043d\u043e:<\/p>\n<pre><code class=\"xml\">&lt;script lang=\"ts\">   import { onMount, setContext } from \"svelte\";   import type { DrawFn } from \".\/types\";    let canvasElement: HTMLCanvasElement;   let fnsToDraw = [] as DrawFn[];    setContext(\"canvas\", {     addDrawFn: (fn: DrawFn) => {       fnsToDraw.push(fn);     },     removeDrawFn: (fn: DrawFn) => {         let index = fnsToDraw.indexOf(fn);         if (index > -1){         fnsToDraw.splice(index, 1);         }     },   });    onMount(() => {     \/\/ get canvas context     let ctx = canvasElement.getContext(\"2d\");     draw(ctx);   });    function draw(ctx){     fnsToDraw.forEach(draw => draw(ctx));   } &lt;\/script>  &lt;canvas bind:this={canvasElement} \/> &lt;slot \/> <\/code><\/pre>\n<p>\u041f\u0435\u0440\u0432\u043e\u0435, \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u2014 \u0448\u0430\u0431\u043b\u043e\u043d \u0438\u0437\u043c\u0435\u043d\u0438\u043b\u0441\u044f, \u0440\u044f\u0434\u043e\u043c \u0441 <code>canvas<\/code> \u043f\u043e\u044f\u0432\u0438\u043b\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442 <code>&lt;slot><\/code>. \u041e\u043d \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0434\u043b\u044f \u043c\u043e\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043b\u044e\u0431\u044b\u0445 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u044e\u0442\u0441\u044f \u0432 <code>canvas<\/code>, \u2014 \u044d\u0442\u043e \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b <code>Line<\/code>. \u042d\u0442\u0438 <code>Line<\/code> \u043d\u0435 \u0434\u043e\u0431\u0430\u0432\u044f\u0442 \u043d\u0438\u043a\u0430\u043a\u0438\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 HTML.<\/p>\n<blockquote>\n<p>\u041c\u0430\u0441\u0441\u0438\u0432 <code>let fnsToDraw = [] as DrawFn[]<\/code> \u0432 <code>&lt;script><\/code> \u0445\u0440\u0430\u043d\u0438\u0442 \u0432\u0441\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043e\u0442\u0440\u0438\u0441\u043e\u0432\u043a\u0438.<\/p>\n<\/blockquote>\n<p>\u041c\u044b \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043b\u0438 \u0438 \u043d\u043e\u0432\u044b\u0439 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442. \u0414\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438. <code>Canvas<\/code> \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0434\u043e <code>Line<\/code>, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0437\u0434\u0435\u0441\u044c \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u0434\u0432\u0430 \u043c\u0435\u0442\u043e\u0434\u0430 \u2014 \u0434\u043b\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438\u0437 <code>DrawFn[]<\/code>. \u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u043b\u044e\u0431\u043e\u0439 \u0438\u0445 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u0431\u0443\u0434\u0435\u0442 \u0438\u043c\u0435\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u0442\u043e\u043c\u0443 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0443 \u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u044b. \u0418\u043c\u0435\u043d\u043d\u043e \u044d\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442\u0441\u044f \u0432 <code>Line<\/code>:<\/p>\n<pre><code class=\"typescript\">&lt;script lang=\"ts\">   import { getContext, onDestroy, onMount } from \"svelte\";   import type { Point, CanvasContext } from \".\/types\";    export let start: Point;   export let end: Point;    let canvasContext = getContext(\"canvas\") as CanvasContext;    onMount(() => {     canvasContext.addDrawFn(draw);   });    onDestroy(() => {     canvasContext.removeDrawFn(draw);   });    function draw(ctx: CanvasRenderingContext2D) {     ctx.beginPath();     ctx.moveTo(...start);     ctx.lineTo(...end);     ctx.stroke();   } &lt;\/script> <\/code><\/pre>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430, \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u043d\u043e\u0433\u043e Canvas, \u043a\u043e\u0433\u0434\u0430 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u043c\u043e\u043d\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u044e \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0438 \u043f\u0440\u0438 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438, \u0432\u0435\u0434\u044c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u0432 \u043b\u044e\u0431\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u043d\u043e \u044f \u043f\u0440\u0435\u0434\u043f\u043e\u0447\u0438\u0442\u0430\u044e \u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e \u043f\u043e\u0441\u043b\u0435 \u043c\u043e\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430. \u041a\u043e\u0433\u0434\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0443\u043d\u0438\u0447\u0442\u043e\u0436\u0430\u0435\u0442\u0441\u044f, \u043e\u043d \u0443\u0434\u0430\u043b\u044f\u0435\u0442 \u0441\u0435\u0431\u044f \u0438\u0437 \u0441\u043f\u0438\u0441\u043a\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u043e\u0442\u0440\u0438\u0441\u043e\u0432\u043a\u0438.<\/p>\n<p>\u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u043c <code>App<\/code> \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430\u043c\u0438 <code>Canvas<\/code> \u0438 <code>Line<\/code>:<\/p>\n<pre><code class=\"xml\">&lt;script lang=\"ts\">   import Canvas from \".\/lib\/Canvas.svelte\";   import Line from \".\/lib\/Line.svelte\"; &lt;\/script>  &lt;main>   &lt;Canvas>     &lt;Line start={[10, 20]} end={[150, 100]} \/>     &lt;Line start={[10, 40]} end={[150, 120]} \/>   &lt;\/Canvas> &lt;\/main><\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/3cf\/64b\/58c\/3cf64b58cc54cc2ea9a60d98d98dd2d9.png\" width=\"1012\" height=\"710\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/3cf\/64b\/58c\/3cf64b58cc54cc2ea9a60d98d98dd2d9.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u041a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 <code>Canvas<\/code> \u043e\u0431\u043d\u043e\u0432\u043b\u0451\u043d \u0434\u043b\u044f \u0434\u0435\u043a\u043b\u0430\u0440\u0430\u0442\u0438\u0432\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u043d\u043e \u0440\u0438\u0441\u0443\u0435\u043c \u043c\u044b \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0440\u0430\u0437, \u043a\u043e\u0433\u0434\u0430 \u043e\u043d \u0441\u043c\u043e\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d.<\/p>\n<p>\u0410 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e, \u0447\u0442\u043e\u0431\u044b canvas \u043e\u0442\u0440\u0438\u0441\u043e\u0432\u044b\u0432\u0430\u043b\u0441\u044f \u0447\u0430\u0441\u0442\u043e \u0438 \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u043b\u0441\u044f \u043f\u0440\u0438 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f\u0445, \u0435\u0441\u043b\u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u0432\u044b \u043d\u0435 \u0445\u043e\u0442\u0438\u0442\u0435 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0433\u043e. \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u0447\u0430\u0441\u0442\u0443\u044e \u043e\u0442\u0440\u0438\u0441\u043e\u0432\u043a\u0443 \u043f\u0440\u0438\u0448\u043b\u043e\u0441\u044c \u0431\u044b \u0434\u0435\u043b\u0430\u0442\u044c \u0441 \u0432\u044b\u0431\u0440\u0430\u043d\u043d\u044b\u043c \u043f\u043e\u0434\u0445\u043e\u0434\u043e\u043c \u0438\u043b\u0438 \u0431\u0435\u0437 \u043d\u0435\u0433\u043e.<\/p>\n<p>\u0418 \u0432\u043e\u0442 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0451\u043d\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0433\u043e <code>canvas<\/code>:<\/p>\n<pre><code class=\"typescript\">&lt;script lang=\"ts\">   \/\/ NOTE: some code removed for readability   \/\/ ...   let frameId: number    \/\/ ...    onMount(() => {     \/\/ get canvas context     let ctx = canvasElement.getContext(\"2d\");     frameId = requestAnimationFrame(() => draw(ctx));   });    onDestroy(() => {     if (frameId){         cancelAnimationFrame(frameId)     }   })    function draw(ctx: CanvasRenderingContext2D) { if clearFrames {     ctx.clearRect(0,0,canvasElement.width, canvasElement.width) }     fnsToDraw.forEach((fn) => fn(ctx));     frameId = requestAnimationFrame(() => draw(ctx))   } &lt;\/script><\/code><\/pre>\n<p>\u042d\u0442\u043e \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0439 \u043e\u0442\u0440\u0438\u0441\u043e\u0432\u043a\u043e\u0439 <code>canvas<\/code> \u0447\u0435\u0440\u0435\u0437 <code>requestAnimationFrame<\/code>. \u041f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0434\u043e \u043f\u0435\u0440\u0435\u0440\u0438\u0441\u043e\u0432\u043a\u0438 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u043e\u043c. \u041d\u043e\u0432\u0430\u044f \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0434\u043b\u044f \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e <code>frameId<\/code> \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043f\u0440\u0438 \u043e\u0442\u043c\u0435\u043d\u0435 \u0430\u043d\u0438\u043c\u0430\u0446\u0438\u0438. \u0417\u0430\u0442\u0435\u043c, \u043a\u043e\u0433\u0434\u0430 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u043c\u043e\u043d\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f, \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f <code>requestAnimationFrame<\/code>, \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0451\u043d\u043d\u044b\u0439 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u043f\u0440\u0438\u0441\u0432\u0430\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430\u0448\u0435\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439. <\/p>\n<p>\u041f\u043e\u043a\u0430 \u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0442\u0430\u043a\u043e\u0439 \u0436\u0435, \u043a\u0430\u043a \u0438 \u0440\u0430\u043d\u044c\u0448\u0435. \u041e\u0442\u043b\u0438\u0447\u0438\u0435 \u2014 \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043e\u0442\u0440\u0438\u0441\u043e\u0432\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u0442 \u043d\u043e\u0432\u044b\u0439 \u043a\u0430\u0434\u0440 \u0430\u043d\u0438\u043c\u0430\u0446\u0438\u0438 \u043f\u043e\u0441\u043b\u0435 \u043a\u0430\u0436\u0434\u043e\u0439 \u043e\u0442\u0440\u0438\u0441\u043e\u0432\u043a\u0438. <code>Canvas<\/code> \u043e\u0447\u0438\u0449\u0430\u0435\u0442\u0441\u044f, \u0430 \u0438\u043d\u0430\u0447\u0435 \u043f\u0440\u0438 \u0430\u043d\u0438\u043c\u0430\u0446\u0438\u0438 \u043a\u0430\u0436\u0434\u044b\u0439 \u043a\u0430\u0434\u0440 \u043e\u0442\u0440\u0438\u0441\u043e\u0432\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043f\u043e\u0432\u0435\u0440\u0445 \u0434\u0440\u0443\u0433\u043e\u0433\u043e. \u042d\u0442\u043e\u0442 \u044d\u0444\u0444\u0435\u043a\u0442 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0436\u0435\u043b\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c \u2014 \u0442\u043e\u0433\u0434\u0430 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0435 <code>clearFrame<\/code> \u0432 <code>false<\/code>. \u041d\u0430\u0448 <code>Canvas<\/code> \u0431\u0443\u0434\u0435\u0442 \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0442\u044c \u043a\u0430\u0436\u0434\u044b\u0439 \u043a\u0430\u0434\u0440 \u0434\u043e \u0443\u043d\u0438\u0447\u0442\u043e\u0436\u0435\u043d\u0438\u044f \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430 \u0438 \u043f\u043e\u0433\u0430\u0448\u0435\u043d\u0438\u044f \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0430\u043d\u0438\u043c\u0430\u0446\u0438\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0441\u043e\u0445\u0440\u0430\u043d\u0451\u043d\u043d\u043e\u0433\u043e \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u0430.<\/p>\n<h3>\u0411\u043e\u043b\u044c\u0448\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438<\/h3>\n<p>\u0411\u0430\u0437\u043e\u0432\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442, \u043d\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0437\u0430\u0445\u043e\u0442\u0435\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u0435\u0433\u043e.<\/p>\n<p>\u0412 \u044d\u0442\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u044b \u0441\u043e\u0431\u044b\u0442\u0438\u044f <code>onmousemove<\/code> \u0438 <code>onmouseleave<\/code>. \u0427\u0442\u043e\u0431\u044b \u043e\u043d\u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u043b\u0438, \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u0435 <code>canvas<\/code> \u0432\u043e\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code class=\"typescript\">&lt;canvas on:mousemove on:mouseleave bind:this={canvasElement} \/><\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u044d\u0442\u0438 \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u043c\u043e\u0436\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0432 <code>App<\/code>:<\/p>\n<pre><code class=\"typescript\">&lt;script lang=\"ts\">   import Canvas from \".\/lib\/Canvas.svelte\";   import Line from \".\/lib\/Line.svelte\";   import type { Point } from \".\/lib\/types\";    function followMouse(e) {     let rect = e.target.getBoundingClientRect();     end = [e.clientX - rect.left, e.clientY - rect.top];   }   let start = [0, 0] as Point;   let end = [0, 0] as Point; &lt;\/script>  &lt;main>   &lt;Canvas     on:mousemove={(e) => followMouse(e)}     on:mouseleave={() => {       end = [0, 0];     }}   >     &lt;Line {start} {end} \/>   &lt;\/Canvas> &lt;\/main><\/code><\/pre>\n<p>Svelte \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0437\u0430 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u043a\u043e\u043d\u0435\u0447\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043b\u0438\u043d\u0438\u0438. \u041d\u043e Canvas \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0433\u043e canvas \u0447\u0435\u0440\u0435\u0437 <code>requestAnimationFrame<\/code>:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/a98\/9bd\/01b\/a989bd01b66124f19f0e8866c418fd19.gif\" width=\"572\" height=\"464\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/a98\/9bd\/01b\/a989bd01b66124f19f0e8866c418fd19.gif\"\/><figcaption><\/figcaption><\/figure>\n<h3>\u0418\u0442\u043e\u0433\u0438<\/h3>\n<p>\u041d\u0430\u0434\u0435\u044e\u0441\u044c, \u044d\u0442\u043e \u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u043e \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u0432\u0430\u043c \u043a\u0430\u043a \u0432\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0432 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435 canvas \u0432 Svelte, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043d\u044f\u0442\u044c, \u043a\u0430\u043a \u043f\u0440\u0435\u0432\u0440\u0430\u0442\u0438\u0442\u044c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 \u0441 \u0438\u043c\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u044b\u043c API \u0432 \u0431\u043e\u043b\u0435\u0435 \u0434\u0435\u043a\u043b\u0430\u0440\u0430\u0442\u0438\u0432\u043d\u0443\u044e.<\/p>\n<p>\u0415\u0441\u0442\u044c \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u0441\u043b\u043e\u0436\u043d\u0435\u0435, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 <a href=\"https:\/\/svelte-cubed.vercel.app\/\">svelte-cubed<\/a> \u0438\u043b\u0438 <a href=\"https:\/\/ngyewch.github.io\/svelte-leaflet\/getting-started\">svelte-leaflet<\/a>. \u0418\u0437 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 <code>svelte-cubed<\/code>:<\/p>\n<p>\u042d\u0442\u043e:<\/p>\n<pre><code class=\"typescript\">import * as THREE from 'three';  function render(element) { const scene = new THREE.Scene(); const camera = new THREE.PerspectiveCamera( 45, element.clientWidth \/ element.clientHeight, 0.1, 2000 );  const renderer = new THREE.WebGLRenderer(); renderer.setSize(element.clientWidth \/ element.clientHeight); element.appendChild(renderer.domElement);  const geometry = new THREE.BoxGeometry(); const material = new THREE.MeshNormalMaterial(); const box = new THREE.Mesh(geometry, material); scene.add(box);  camera.position.x = 2; camera.position.y = 2; camera.position.z = 5;  camera.lookAt(new THREE.Vector3(0, 0, 0));  renderer.render(scene, camera); }<\/code><\/pre>\n<p>\u041f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432:<\/p>\n<pre><code class=\"xml\">&lt;script> import * as THREE from 'three'; import * as SC from 'svelte-cubed'; &lt;\/script>  &lt;SC.Canvas> &lt;SC.Mesh geometry={new THREE.BoxGeometry()} \/> &lt;SC.PerspectiveCamera position={[1, 1, 3]} \/> &lt;\/SC.Canvas><\/code><\/pre>\n<p>Canvas API \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0441\u0448\u0438\u0440\u0438\u0442\u044c \u0438 \u0434\u0430\u0436\u0435 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443. <\/p>\n<p>\u0410 \u043c\u044b \u043f\u043e\u043c\u043e\u0436\u0435\u043c \u043f\u0440\u043e\u043a\u0430\u0447\u0430\u0442\u044c \u0432\u0430\u0448\u0438 \u043d\u0430\u0432\u044b\u043a\u0438 \u0438\u043b\u0438 \u0441 \u0441\u0430\u043c\u043e\u0433\u043e \u043d\u0430\u0447\u0430\u043b\u0430 \u043e\u0441\u0432\u043e\u0438\u0442\u044c \u043f\u0440\u043e\u0444\u0435\u0441\u0441\u0438\u044e, \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u0443\u044e \u0432 \u043b\u044e\u0431\u043e\u0435 \u0432\u0440\u0435\u043c\u044f:<\/p>\n<ul>\n<li>\n<p><a href=\"https:\/\/skillfactory.ru\/frontend-razrabotchik?utm_source=habr&amp;utm_medium=habr&amp;utm_campaign=article&amp;utm_content=coding_fr_100822&amp;utm_term=conc\"><u>\u041f\u0440\u043e\u0444\u0435\u0441\u0441\u0438\u044f Frontend-\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a<\/u><\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/skillfactory.ru\/data-scientist-pro?utm_source=habr&amp;utm_medium=habr&amp;utm_campaign=article&amp;utm_content=data-science_dspr_100822&amp;utm_term=conc\"><u>\u041f\u0440\u043e\u0444\u0435\u0441\u0441\u0438\u044f Data Scientist<\/u><\/a><\/p>\n<\/li>\n<\/ul>\n<figure class=\"\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/b16\/dac\/539\/b16dac539a0a09cd1c28951311a610c7.png\" width=\"auto\" height=\"auto\" data-src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/b16\/dac\/539\/b16dac539a0a09cd1c28951311a610c7.png\"\/><figcaption><\/figcaption><\/figure>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/company\/skillfactory\/blog\/681866\/\"> https:\/\/habr.com\/ru\/company\/skillfactory\/blog\/681866\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"\"><figcaption><\/figcaption><\/figure>\n<p>\u0420\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u0438\u0437 \u043a\u043e\u043d\u0441\u0430\u043b\u0442\u0438\u043d\u0433\u043e\u0432\u043e\u0439 \u043a\u043e\u043c\u043f\u0430\u043d\u0438\u0438 \u0432 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438 This Dot Labs \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442, \u043a\u0430\u043a \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c canvas \u0432 Svelte \u0438 \u043a\u0430\u043a \u043f\u0440\u0435\u0432\u0440\u0430\u0442\u0438\u0442\u044c \u043c\u043d\u043e\u0433\u043e\u0441\u043b\u043e\u0432\u043d\u044b\u0439 API Canvas \u0432 \u043a\u0440\u0430\u0442\u043a\u0438\u0439, \u0431\u043e\u043b\u0435\u0435 \u0434\u0435\u043a\u043b\u0430\u0440\u0430\u0442\u0438\u0432\u043d\u044b\u0439. \u041f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0441\u0442\u0438 \u2014 \u043a \u0441\u0442\u0430\u0440\u0442\u0443 \u043d\u0430\u0448\u0435\u0433\u043e <a href=\"https:\/\/skillfactory.ru\/frontend-razrabotchik?utm_source=habr&amp;utm_medium=habr&amp;utm_campaign=article&amp;utm_content=coding_fr_100822&amp;utm_term=lead\"><u>\u043a\u0443\u0440\u0441\u0430 \u043f\u043e \u0444\u0440\u043e\u043d\u0442\u0435\u043d\u0434\u0443<\/u><\/a>.<\/p>\n<hr\/>\n<p>\u042d\u043b\u0435\u043c\u0435\u043d\u0442 <code>&lt;canvas><\/code> \u0438 Canvas API \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0440\u0438\u0441\u043e\u0432\u0430\u0442\u044c \u043d\u0430 JavaScript, \u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e Svelte \u0435\u0433\u043e \u0438\u043c\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u044b\u0439 API \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u0434\u0435\u043a\u043b\u0430\u0440\u0430\u0442\u0438\u0432\u043d\u044b\u0439. \u042d\u0442\u043e \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u043e\u0442 \u0432\u0430\u0441 \u0437\u043d\u0430\u043d\u0438\u044f Renderless-\u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432 \u2014 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435 \u043e\u0442\u0440\u0438\u0441\u043e\u0432\u044b\u0432\u0430\u044e\u0442\u0441\u044f.<\/p>\n<h3>Renderless<\/h3>\n<p>\u0412\u0441\u0435 \u0440\u0430\u0437\u0434\u0435\u043b\u044b \u0444\u0430\u0439\u043b\u0430 <code>.svelte<\/code>, \u0432\u043a\u043b\u044e\u0447\u0430\u044f \u0448\u0430\u0431\u043b\u043e\u043d, \u043d\u0435\u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u044b. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0435 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0435\u0442\u0441\u044f, \u043d\u043e \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043b\u043e\u0433\u0438\u043a\u0443 \u0432 \u0442\u0435\u0433\u0435 <code>&lt;script><\/code>.<\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043d\u043e\u0432\u044b\u0439 \u043f\u0440\u043e\u0435\u043a\u0442 Svelte \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e Vite:<\/p>\n<pre><code class=\"bash\">npm init vite   Project name: canvas-svelte  Select a framework: \u203a svelte  Select a variant: \u203a svelte-ts  cd canvas-svelte npm i<\/code><\/pre>\n<p>\u0418 \u043d\u043e\u0432\u044b\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u2014 <code>Renderless<\/code>:<\/p>\n<pre><code class=\"xml\">&lt;!-- src\/lib\/Renderless.svelte --> &lt;script>     console.log(\"No template\"); &lt;\/script><\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430 \u0432\u044b\u0432\u0435\u0434\u0435\u043c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043f\u0435\u0440\u0435\u043f\u0438\u0448\u0435\u043c \u0442\u043e\u0447\u043a\u0443 \u0432\u0445\u043e\u0434\u0430 <code>App<\/code>:<\/p>\n<pre><code class=\"typescript\">\/\/ src\/main.ts \/\/ import App from '.\/App.svelte' import Renderless from '.\/lib\/Renderless.svelte'  const app = new Renderless({   target: document.getElementById('app') })  export default app<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0441\u0435\u0440\u0432\u0435\u0440, \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u0435\u043c \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430 \u2014 \u0438 \u0432\u0438\u0434\u0438\u043c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435:<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u0420\u0430\u0431\u043e\u0442\u0430\u0435\u0442.<\/p>\n<blockquote>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u043c\u0435\u0442\u043e\u0434\u044b \u0436\u0438\u0437\u043d\u0435\u043d\u043d\u043e\u0433\u043e \u0446\u0438\u043a\u043b\u0430 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430 \u043f\u043e-\u043f\u0440\u0435\u0436\u043d\u0435\u043c\u0443 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b, \u0442\u043e \u0435\u0441\u0442\u044c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u0432\u0435\u0434\u0451\u0442 \u0441\u0435\u0431\u044f \u043a\u0430\u043a \u043e\u0431\u044b\u0447\u043d\u044b\u0439, \u0434\u0430\u0436\u0435 \u043a\u043e\u0433\u0434\u0430 \u0443 \u043d\u0435\u0433\u043e \u043d\u0435\u0442 \u0448\u0430\u0431\u043b\u043e\u043d\u0430.<\/p>\n<\/blockquote>\n<p>\u041f\u0440\u043e\u0432\u0435\u0440\u0438\u043c \u044d\u0442\u043e:<\/p>\n<pre><code class=\"typescript\">&lt;!-- src\/lib\/Renderless.svelte --> &lt;script>     import { onMount } from \"svelte\";     console.log(\"No template\");     onMount(() => {         console.log(\"Component mounted\");     }); &lt;\/script><\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u043c\u043e\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f Renderless \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0435\u0442 \u0432\u0442\u043e\u0440\u043e\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435, \u043e\u0431\u0430 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u0432\u044b\u0432\u043e\u0434\u044f\u0442\u0441\u044f \u0432 \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435:<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e Renderless \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u0430\u043a \u043b\u044e\u0431\u043e\u0439 \u0434\u0440\u0443\u0433\u043e\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 Svelte. \u0412\u0435\u0440\u043d\u0451\u043c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f <code>main.ts<\/code> \u0438 &#171;\u043e\u0442\u0440\u0438\u0441\u0443\u0435\u043c&#187; \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u0432\u043d\u0443\u0442\u0440\u0438 <code>App<\/code>:<\/p>\n<pre><code class=\"typescript\">\/\/ src\/main.ts import App from '.\/App.svelte'  const app = new App({   target: document.getElementById('app') })  export default app<\/code><\/pre>\n<pre><code class=\"xml\">&lt;!-- src\/App.svelte --> &lt;script lang=\"ts\">   import { onMount } from \"svelte\";    import Renderless from \".\/lib\/Renderless.svelte\";   console.log(\"App: initialized\");   onMount(() => {     console.log(\"App: mounted\");   }); &lt;\/script>  &lt;main>   &lt;Renderless \/> &lt;\/main><\/code><\/pre>\n<p>\u041f\u0435\u0440\u0435\u043f\u0438\u0448\u0435\u043c Renderless, \u0447\u0442\u043e\u0431\u044b \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0430\u0436\u043d\u044b\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f:<\/p>\n<pre><code class=\"xml\">&lt;!-- src\/lib\/Renderless.svelte --> &lt;script>     import { onMount } from \"svelte\";     console.log(\"Renderless: initialized\");     onMount(() => {         console.log(\"Renderless: mounted\");     }); &lt;\/script><\/code><\/pre>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u041f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432 \u0431\u0435\u0437 \u043e\u0442\u0440\u0438\u0441\u043e\u0432\u043a\u0438 \u0438 Canvas \u0432\u0430\u0436\u043d\u043e \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0438 \u043c\u043e\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432.<\/p>\n<p>\u0415\u0449\u0451 \u043e\u0434\u0438\u043d \u0441\u043f\u043e\u0441\u043e\u0431 \u043c\u043e\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u2014 \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0435\u0433\u043e \u043a\u0430\u043a \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0434\u0440\u0443\u0433\u043e\u0433\u043e \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430. \u0422\u0430\u043a\u0430\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0435\u043a\u0446\u0438\u0435\u0439 \u043a\u043e\u043d\u0442\u0435\u043d\u0442\u0430. \u042d\u0442\u0443 \u043f\u0440\u043e\u0435\u043a\u0446\u0438\u044e \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>slot<\/code>.<\/p>\n<p>\u041d\u0430\u043f\u0438\u0448\u0435\u043c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 <code>Container<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u0440\u0438\u0441\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0435 \u0432 \u0441\u043b\u043e\u0442 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b:<\/p>\n<pre><code class=\"typescript\">&lt;!-- src\/lib\/Container.svelte --> &lt;script>     import { onMount } from \"svelte\";     console.log(\"Container: initialized\");     onMount(() => {         console.log(\"Container: mounted\");     }); &lt;\/script>  &lt;h1>The container of things&lt;\/h1> &lt;slot \/> &lt;p>invisible things&lt;\/p><\/code><\/pre>\n<p>\u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>prop<\/code> \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0432 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 <code>Renderless<\/code> \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440:<\/p>\n<pre><code class=\"xml\">&lt;!-- src\/lib\/Renderless.svelte --> &lt;script lang=\"ts\">     import { onMount } from \"svelte\";     export let id:string = \"NoId\"     console.log(`Renderless ${id}: initialized`);     onMount(() => {         console.log(`Renderless ${id}: mounted`);     }); &lt;\/script> <\/code><\/pre>\n<p>\u041f\u0435\u0440\u0435\u043f\u0438\u0448\u0435\u043c <code>App<\/code> \u0434\u043b\u044f \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u0430, \u0437\u0430\u0442\u0435\u043c \u043f\u0435\u0440\u0435\u0434\u0430\u0434\u0438\u043c \u0432 <code>App<\/code> \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u043e\u0432 <code>Renderless<\/code>:<\/p>\n<pre><code class=\"typescript\">&lt;!-- src\/App.svelte --> &lt;script lang=\"ts\">   import { onMount } from \"svelte\";   import Container from \".\/lib\/Container.svelte\";    import Renderless from \".\/lib\/Renderless.svelte\";   console.log(\"App: initialized\");   onMount(() => {     console.log(\"App: mounted\");   }); &lt;\/script>  &lt;main>   &lt;Container>     &lt;Renderless id=\"Foo\"\/>     &lt;Renderless id=\"Bar\"\/>     &lt;Renderless id=\"Baz\"\/>   &lt;\/Container> &lt;\/main><\/code><\/pre>\n<p>\u041d\u0438\u0436\u0435 \u0432\u0438\u0434\u043d\u043e \u0438 <code>Container<\/code>, \u0438 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b \u0431\u0435\u0437 \u043e\u0442\u0440\u0438\u0441\u043e\u0432\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0440\u0438 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0438 \u043c\u043e\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u043f\u0438\u0448\u0443\u0442 \u0432 \u043b\u043e\u0433:<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430\u043c\u0438 \u0431\u0435\u0437 \u043e\u0442\u0440\u0438\u0441\u043e\u0432\u043a\u0438 \u0432 \u0441\u043e\u0447\u0435\u0442\u0430\u043d\u0438\u0438 \u0441 <code>&lt;canvas><\/code>.<\/p>\n<h3>HTML canvas \u0438 Canvas API<\/h3>\n<p>\u042d\u043b\u0435\u043c\u0435\u043d\u0442 <code>canvas<\/code> \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043d\u0438\u043a\u0430\u043a\u0438\u0445 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u043a\u0440\u043e\u043c\u0435 \u0440\u0435\u0437\u0435\u0440\u0432\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0434\u043b\u044f \u043e\u0442\u0440\u0438\u0441\u043e\u0432\u043a\u0438. \u0412\u0441\u0451, \u0447\u0442\u043e \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u0432 canvas, \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043e \u043d\u0430 \u0438\u043c\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u043e\u043c API.<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043d\u043e\u0432\u044b\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 Canvas \u0438 \u043e\u0442\u0440\u0438\u0441\u0443\u0435\u043c canvas:<\/p>\n<pre><code class=\"xml\">&lt;!-- src\/lib\/Canvas.svelte --> &lt;script>     import { onMount } from \"svelte\";      console.log(\"Canvas: initialized\");     onMount(() => {         console.log(\"Canvas: mounted\");     }); &lt;\/script>  &lt;canvas \/><\/code><\/pre>\n<p>\u041e\u0431\u043d\u043e\u0432\u0438\u043c <code>App<\/code>, \u0447\u0442\u043e\u0431\u044b \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f <code>Canvas<\/code>: <\/p>\n<pre><code class=\"xml\">&lt;!-- src\/App.svelte --> &lt;script lang=\"ts\">   import { onMount } from \"svelte\";   import Canvas from \".\/lib\/Canvas.svelte\";    console.log(\"App: initialized\");   onMount(() => {     console.log(\"App: mounted\");   }); &lt;\/script>  &lt;main>  &lt;Canvas \/> &lt;\/main><\/code><\/pre>\n<p>\u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u043e\u0442\u043a\u0440\u043e\u0435\u043c \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430:<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<h4>\u041e\u0442\u0440\u0438\u0441\u043e\u0432\u043a\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432\u043d\u0443\u0442\u0440\u0438 canvas<\/h4>\n<p>\u041a\u0430\u043a \u0443\u0436\u0435 \u0433\u043e\u0432\u043e\u0440\u0438\u043b\u043e\u0441\u044c, \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u0440\u044f\u043c\u043e \u0432 canvas \u043d\u0435\u043b\u044c\u0437\u044f. \u0427\u0442\u043e\u0431\u044b \u0440\u0438\u0441\u043e\u0432\u0430\u0442\u044c, \u043d\u0443\u0436\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 API.<\/p>\n<p>\u0421\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0447\u0435\u0440\u0435\u0437 <code>bind:this<\/code>. \u0412\u0430\u0436\u043d\u043e \u043f\u043e\u043d\u0438\u043c\u0430\u0442\u044c, \u0447\u0442\u043e \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 API \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d, \u0442\u043e \u0435\u0441\u0442\u044c \u0440\u0438\u0441\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u0438\u0434\u0451\u0442\u0441\u044f \u043f\u043e\u0441\u043b\u0435 \u043c\u043e\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430:<\/p>\n<pre><code class=\"xml\">&lt;script lang=\"ts\">     import { onMount } from \"svelte\";     let canvasElement: HTMLCanvasElement     console.log(\"1\", canvasElement) \/\/ undefined!!!     console.log(\"Canvas: initialized\");     onMount(() => {         console.log(\"2\", canvasElement) \/\/ OK!!!         console.log(\"Canvas: mounted\");     }); &lt;\/script>  &lt;canvas bind:this={canvasElement}\/><\/code><\/pre>\n<p>\u041d\u0430\u0440\u0438\u0441\u0443\u0435\u043c \u043b\u0438\u043d\u0438\u044e. \u0414\u043b\u044f \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u043e\u0441\u0442\u0438 \u044f \u0443\u0431\u0440\u0430\u043b \u0432\u0441\u0451 \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435:<\/p>\n<pre><code class=\"xml\">&lt;script lang=\"ts\">     import { onMount } from \"svelte\";     let canvasElement: HTMLCanvasElement     onMount(() => {         \/\/ get canvas context         let ctx = canvasElement.getContext(\"2d\")          \/\/ draw line         ctx.beginPath();         ctx.moveTo(10, 20); \/\/ line will start here         ctx.lineTo(150, 100); \/\/ line ends here         ctx.stroke(); \/\/ draw it     }); &lt;\/script>  &lt;canvas bind:this={canvasElement}\/><\/code><\/pre>\n<p>\u0427\u0442\u043e\u0431\u044b \u0440\u0438\u0441\u043e\u0432\u0430\u0442\u044c, canvas \u043d\u0443\u0436\u0435\u043d \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442, \u0442\u0430\u043a \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0441\u043b\u0435 \u043c\u043e\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430:<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u0415\u0441\u043b\u0438 \u0437\u0430\u0445\u043e\u0447\u0435\u0442\u0441\u044f \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432\u0442\u043e\u0440\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443, \u043f\u0440\u0438\u0434\u0451\u0442\u0441\u044f \u0434\u043e\u043f\u0438\u0441\u0430\u0442\u044c \u0438 \u043d\u043e\u0432\u044b\u0439 \u0431\u043b\u043e\u043a \u043a\u043e\u0434\u0430:<\/p>\n<pre><code class=\"xml\">&lt;script lang=\"ts\">     import { onMount } from \"svelte\";     let canvasElement: HTMLCanvasElement     onMount(() => {         \/\/ get canvas context         let ctx = canvasElement.getContext(\"2d\")          \/\/ draw first line         ctx.beginPath();         ctx.moveTo(10, 20); \/\/ line will start here         ctx.lineTo(150, 100); \/\/ line ends here         ctx.stroke(); \/\/ draw it         \/\/ draw second line         ctx.beginPath();         ctx.moveTo(10, 40); \/\/ line will start here         ctx.lineTo(150, 120); \/\/ line ends here         ctx.stroke(); \/\/ draw it     }); &lt;\/script><\/code><\/pre>\n<p>\u041c\u044b \u0440\u0438\u0441\u0443\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u044b\u0435 \u0444\u0438\u0433\u0443\u0440\u044b \u2014 \u0430 \u043a\u043e\u0434\u0430 \u0432 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0435 \u0432\u0441\u0451 \u0431\u043e\u043b\u044c\u0448\u0435 \u0438 \u0431\u043e\u043b\u044c\u0448\u0435. \u041c\u043e\u0436\u043d\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0441\u043e\u043a\u0440\u0430\u0449\u0430\u044e\u0449\u0438\u0435 \u043a\u043e\u0434 \u043b\u0438\u043d\u0438\u0439:<\/p>\n<pre><code class=\"typescript\">&lt;script lang=\"ts\">     import { onMount } from \"svelte\";     let canvasElement: HTMLCanvasElement;     onMount(() => {         \/\/ get canvas context         let ctx = canvasElement.getContext(\"2d\");          \/\/ draw first line         drawLine(ctx, [10, 20], [150, 100]);          \/\/ draw second line         drawLine(ctx, [10, 40], [150, 120]);     });      type Point = [number, number];     function drawLine(ctx: CanvasRenderingContext2D, start: Point, end: Point) {         ctx.beginPath();         ctx.moveTo(...start); \/\/ line will start here         ctx.lineTo(...end); \/\/ line ends here         ctx.stroke(); \/\/ draw it     } &lt;\/script>  &lt;canvas bind:this={canvasElement} \/><\/code><\/pre>\n<p>\u0427\u0438\u0442\u0430\u0442\u044c \u043a\u043e\u0434 \u043b\u0435\u0433\u0447\u0435, \u043d\u043e \u0432\u0441\u044f \u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u043f\u043e-\u043f\u0440\u0435\u0436\u043d\u0435\u043c\u0443 \u0434\u0435\u043b\u0435\u0433\u0438\u0440\u0443\u0435\u0442\u0441\u044f <code>Canvas<\/code>, \u0430 \u044d\u0442\u043e \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442 \u043a \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430. \u0418\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043f\u043e\u043c\u043e\u0433\u0443\u0442 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b \u0431\u0435\u0437 \u043e\u0442\u0440\u0438\u0441\u043e\u0432\u043a\u0438 \u0438 API Context.<\/p>\n<p>\u0418 \u0432\u043e\u0442 \u0447\u0442\u043e \u043d\u0430\u043c \u0443\u0436\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e:<\/p>\n<ul>\n<li>\n<p>\u0414\u043b\u044f \u0440\u0438\u0441\u043e\u0432\u0430\u043d\u0438\u044f \u043d\u0430\u043c \u043d\u0443\u0436\u0435\u043d Canvas.<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043b\u0435 \u043c\u043e\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430.<\/p>\n<\/li>\n<li>\n<p>\u0414\u043e\u0447\u0435\u0440\u043d\u0438\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b \u043c\u043e\u043d\u0442\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u043f\u0435\u0440\u0435\u0434 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u043c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u043c.<\/p>\n<\/li>\n<li>\n<p>\u0420\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u043f\u0435\u0440\u0435\u0434 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u043c\u0438 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430\u043c\u0438.<\/p>\n<\/li>\n<li>\n<p>\u0414\u043e\u0447\u0435\u0440\u043d\u0438\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u0438 \u043c\u043e\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438.<\/p>\n<\/li>\n<\/ul>\n<p>\u041d\u0430\u0448 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u043d\u0443\u0436\u043d\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c \u043d\u0430 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432. \u0417\u0434\u0435\u0441\u044c \u0445\u043e\u0447\u0435\u0442\u0441\u044f, \u0447\u0442\u043e\u0431\u044b <code>Line<\/code> \u0440\u0438\u0441\u043e\u0432\u0430\u043b \u0441\u0430\u043c \u0441\u0435\u0431\u044f.<\/p>\n<p><code>Canvas<\/code> \u0438 <code>Line<\/code> \u0441\u0432\u044f\u0437\u0430\u043d\u044b. <code>Line<\/code> \u043d\u0435\u043b\u044c\u0437\u044f \u043e\u0442\u0440\u0438\u0441\u043e\u0432\u0430\u0442\u044c \u0431\u0435\u0437 Canvas, \u0438 \u0435\u043c\u0443 \u043d\u0443\u0436\u0435\u043d \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 <code>canvas<\/code>. \u041d\u043e \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u043d\u0435\u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d, \u043a\u043e\u0433\u0434\u0430 \u043c\u043e\u043d\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442, \u0432\u0435\u0434\u044c <code>Line<\/code> \u043c\u043e\u043d\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043f\u0435\u0440\u0435\u0434 <code>Canvas<\/code>. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u043e\u0434\u0445\u043e\u0434 \u043d\u0443\u0436\u0435\u043d \u0434\u0440\u0443\u0433\u043e\u0439.<\/p>\n<p>\u0412\u043c\u0435\u0441\u0442\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u0434\u043b\u044f \u043e\u0442\u0440\u0438\u0441\u043e\u0432\u043a\u0438 \u0441\u0430\u043c\u043e\u0433\u043e \u0441\u0435\u0431\u044f \u0441\u043e\u043e\u0431\u0449\u0438\u043c \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u043c\u0443 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0443, \u0447\u0442\u043e \u0440\u0438\u0441\u043e\u0432\u0430\u0442\u044c \u043d\u0443\u0436\u043d\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442. <code>Canvas<\/code> \u0438 <code>Line<\/code> \u0441\u043e\u0435\u0434\u0438\u043d\u0438\u043c \u0447\u0435\u0440\u0435\u0437 <code>Context<\/code>.<\/p>\n<p><code>Context<\/code> \u2014 \u044d\u0442\u043e \u0441\u043f\u043e\u0441\u043e\u0431 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u0434\u0432\u0443\u0445 \u0438 \u0431\u043e\u043b\u0435\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432. \u0415\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0438\u043b\u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438, \u0430 \u044d\u0442\u043e \u043d\u0430\u043c \u0438 \u043d\u0443\u0436\u043d\u043e: <code>Canvas<\/code> \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043f\u0435\u0440\u0435\u0434 <code>Line<\/code>.<\/p>\n<p>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u0435\u0440\u0435\u043d\u0435\u0441\u0451\u043c \u043e\u0442\u0440\u0438\u0441\u043e\u0432\u043a\u0443 \u043b\u0438\u043d\u0438\u0438 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442, \u0430 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0442\u0438\u043f\u044b \u2014 \u0432 \u0438\u0445 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0444\u0430\u0439\u043b, \u0447\u0442\u043e\u0431\u044b \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0438\u0445 \u043e\u0431\u0449\u0438\u043c\u0438 \u0434\u043b\u044f \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432:<\/p>\n<pre><code class=\"typescript\">\/\/ src\/types.ts export type Point = [number, number]; export type DrawFn = (ctx: CanvasRenderingContext2D) => void; export type CanvasContext = {   addDrawFn: (fn: DrawFn) => void;   removeDrawFn: (fn: DrawFn) => void; };<\/code><\/pre>\n<pre><code class=\"xml\">&lt;!-- src\/lib\/Line.svelte --> &lt;script lang=\"ts\">     import type { Point } from \".\/types\";      export let start: Point;     export let end: Point;      function draw(ctx: CanvasRenderingContext2D) {         ctx.beginPath();         ctx.moveTo(...start);         ctx.lineTo(...end);         ctx.stroke();     } &lt;\/script><\/code><\/pre>\n<p>\u042d\u0442\u043e \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u0445\u043e\u0436\u0435 \u043d\u0430 \u0442\u043e, \u0447\u0442\u043e \u0431\u044b\u043b\u043e \u0432 <code>Canvas<\/code>, \u043d\u043e \u0430\u0431\u0441\u0442\u0440\u0430\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u043e \u0434\u043e \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430. \u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0443\u0436\u043d\u043e \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u043c\u043c\u0443\u043d\u0438\u043a\u0430\u0446\u0438\u044e <code>Canvas<\/code> \u0438 <code>Line<\/code>.<\/p>\n<p><code>Canvas<\/code> \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043a\u0430\u043a \u043e\u0440\u043a\u0435\u0441\u0442\u0440\u0430\u0442\u043e\u0440 \u0432\u0441\u0435\u0439 \u043e\u0442\u0440\u0438\u0441\u043e\u0432\u043a\u0438. \u041e\u043d \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u0442 \u0432\u0441\u0435 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b, \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043e\u0442\u0440\u0438\u0441\u043e\u0432\u043a\u0438 \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u043e\u0442\u0440\u0438\u0441\u043e\u0432\u043a\u0443, \u043a\u043e\u0433\u0434\u0430 \u044d\u0442\u043e \u043d\u0443\u0436\u043d\u043e:<\/p>\n<pre><code class=\"xml\">&lt;script lang=\"ts\">   import { onMount, setContext } from \"svelte\";   import type { DrawFn } from \".\/types\";    let canvasElement:<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-336871","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/336871","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=336871"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/336871\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=336871"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=336871"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=336871"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}