{"id":299221,"date":"2020-02-25T09:00:49","date_gmt":"2020-02-25T09:00:49","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=299221"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=299221","title":{"rendered":"Prerender on canvas 2D"},"content":{"rendered":"\n<div class=\"post__text post__text-html\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/489602\/\">\n<p>\u0412 \u043f\u043e\u043f\u044b\u0442\u043a\u0430\u0445 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c 2D \u0430\u043d\u0438\u043c\u0430\u0446\u0438\u044e \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u0443\u044e \u0432 canvas, \u0431\u044b\u043b \u043d\u0430\u0439\u0434\u0435\u043d \u043e\u0434\u0438\u043d \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442.<\/p>\n<p>  <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/tn\/tw\/jl\/tntwjl946vq-09hyhxmyraiffuu.jpeg\" alt=\"hero image\"><\/p>\n<p>  <\/p>\n<p>\u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0432\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u2014 prerender.<\/p>\n<p>  <\/p>\n<p>&quot;\u0410 \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0432\u0441\u0435 \u043a\u0430\u0434\u0440\u044b \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0438 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u0438\u0445 \u043f\u043e\u0441\u043b\u0435 \u043e\u043a\u043e\u043d\u0447\u0430\u043d\u0438\u044f \u0430\u043d\u0438\u043c\u0430\u0446\u0438\u0438?&quot; \u2014 \u043f\u043e\u0434\u0443\u043c\u0430\u043b\u0438 \u043c\u044b \u0441 \u0442\u043e\u0432\u0430\u0440\u0438\u0449\u0435\u043c \u0438 \u0432\u043e\u0442 \u0447\u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0434\u0435\u043d\u044c.<\/p>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<h2 id=\"live-demo\">Live demo<\/h2>\n<p>  <\/p>\n<p><a href=\"http:\/\/amedomary.ru\/prerender-canvas\/\">amedomary.ru\/prerender-canvas<\/a><\/p>\n<p>  <\/p>\n<h2 id=\"code-v10\">Code v1.0<\/h2>\n<p>  <\/p>\n<p>\u0421\u0430\u043c\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0430 \u0435\u0441\u0442\u044c \u043d\u0430 <a href=\"https:\/\/github.com\/Amedomary\/prerender-canvas\">Github<\/a>.<\/p>\n<p>  <\/p>\n<p><strong>\u041b\u043e\u0433\u0438\u043a\u0430 \u043e\u0447\u0435\u043d\u044c \u043f\u0440\u043e\u0441\u0442\u0430 \u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u0441\u044f \u0432 2 \u0448\u0430\u0433\u0430:<\/strong><\/p>\n<p>  <\/p>\n<p>\u0428\u0430\u0433 1 \u2014 \u0421\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u043a\u0430\u0436\u0434\u044b\u0439 \u043a\u0430\u0434\u0440 \u043d\u0430\u0448\u0435\u0439 \u0430\u043d\u0438\u043c\u0430\u0446\u0438\u0438 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439<\/p>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0428\u0430\u0433 1 &#8212; javascript \u043a\u043e\u0434<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"javascript\">  function renderLoop() {     draw();      \/\/ create prerender images     canvasPrerender.toBlob(blob =&gt; {       const reader = new FileReader();       reader.readAsDataURL(blob);        reader.onload = function() {         const image = document.createElement(&quot;img&quot;);         image.src = reader.result;         prerenderArr.push(image);       };     });      if (prerenderIsCompleted) {       requestAnimationFrame(renderLoop);     } else {       requestAnimationFrame(drawPreImg);     }   }<\/code><\/pre>\n<\/div>\n<\/div>\n<p>  <\/p>\n<p>\u0428\u0430\u0433 2 \u2014 \u041f\u043e\u0441\u043b\u0435 \u043e\u043a\u043e\u043d\u0447\u0430\u043d\u0438\u044f \u2014 \u0440\u0438\u0441\u0443\u0435\u043c \u0430\u043d\u0438\u043c\u0430\u0446\u0438\u044e \u0438\u0437 \u0443\u0436\u0435 \u0433\u043e\u0442\u043e\u0432\u044b\u0445 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439<\/p>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0428\u0430\u0433 2 &#8212; javascript \u043a\u043e\u0434<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"javascript\">  function drawPreImg() {     ctx.clearRect(0, 0, canvas.width, canvas.height);     ctx.drawImage(prerenderArr[i], 0, 0);      requestAnimationFrame(drawPreImg);   }<\/code><\/pre>\n<\/div>\n<\/div>\n<p>  <\/p>\n<p><a href=\"https:\/\/github.com\/Amedomary\/prerender-canvas\/blob\/master\/script.js\">\u0412\u0435\u0441\u044c \u043a\u043e\u0434 \u0446\u0435\u043b\u0438\u043a\u043e\u043c<\/a><\/p>\n<p>  <\/p>\n<h2 id=\"analytic\">Analytic<\/h2>\n<p>  <\/p>\n<p><em>\u041a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0438\u043d\u0434\u0438\u0432\u0438\u0434\u0443\u0430\u043b\u044c\u043d\u044b \u0438 \u0437\u0430\u0432\u0438\u0441\u044f\u0442 \u043e\u0442 \u043c\u043e\u0449\u043d\u043e\u0441\u0442\u0438 \u043c\u0430\u0448\u0438\u043d\u044b. \u042f \u0442\u0435\u0441\u0442\u0438\u0440\u0443\u044e \u043d\u0430 macbook 13 2019 \u0433\u043e\u0434\u0430.<\/em><\/p>\n<p>  <\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u043a\u043e\u0434 \u0438 \u0437\u0430\u0442\u0440\u0430\u0442\u044b \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432 \u0432 \u0442\u043e\u043c \u0447\u0438\u0441\u043b\u0435 \u0438 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445.<\/p>\n<p>  <\/p>\n<p><strong>FPS<\/strong><\/p>\n<p>  <\/p>\n<p>Fps &quot;\u0434\u043e&quot; \u0440\u0430\u0432\u0435\u043d \u0447\u0430\u0441\u0442\u043e\u0442\u0435 \u043a\u0430\u0434\u0440\u043e\u0432 \u0435\u0441\u043b\u0438 \u0431\u044b \u043c\u044b \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043b\u0438 \u0430\u043d\u0438\u043c\u0430\u0446\u0438\u044e \u0441\u0440\u0430\u0437\u0443 \u0431\u0435\u0437 \u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u043e\u0442\u0440\u0438\u0441\u043e\u0432\u043a\u0438.<\/p>\n<p>  <\/p>\n<p>Fps &quot;\u0434\u043e&quot; \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0430\u043d\u0438\u043c\u0430\u0446\u0438\u0438 (prerender): 14-22<br \/>  Fps &quot;\u043f\u043e\u0441\u043b\u0435&quot; (\u0441\u0430\u043c\u0430 \u0430\u043d\u0438\u043c\u0430\u0446\u0438\u044f): 58-60<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/fe\/cm\/gk\/fecmgkk4u0omkvror_0xwdbrcva.png\" alt=\"fps counter\" width=\"60%\">  <\/p>\n<p>\u041d\u0430 \u0441\u043a\u0440\u0438\u043d\u0448\u043e\u0442\u0435 \u0432\u044b\u0448\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u043e\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435, \u0447\u0435\u043c \u0432 <a href=\"http:\/\/amedomary.ru\/prerender-canvas\/\">live demo<\/a>.<\/p>\n<p>  <\/p>\n<p><strong>\u0412\u0440\u0435\u043c\u044f<\/strong><\/p>\n<p>  <\/p>\n<p>Prerender \u2014 9 \u0441\u0435\u043a\u0443\u043d\u0434<br \/>  \u0421\u0430\u043c\u0430 \u0430\u043d\u0438\u043c\u0430\u0446\u0438\u044f \u2014 3 \u0441\u0435\u043a\u0443\u043d\u0434\u044b<br \/>  \u041e\u0431\u0449\u0435\u0435 \u2014 12 \u0441\u0435\u043a\u0443\u043d\u0434 (\u0442\u0430\u043a \u0436\u0435 \u043d\u0430 <a href=\"http:\/\/amedomary.ru\/prerender-canvas\/\">live demo<\/a>)<\/p>\n<p>  <\/p>\n<p><strong>CPU \/ Performance<\/strong><\/p>\n<p>  <\/p>\n<p>\u041a\u0430\u043a \u0432\u0438\u0434\u043d\u043e \u0438\u0437 \u0441\u043a\u0440\u0438\u043d\u0448\u043e\u0442\u0430 \u043d\u0438\u0436\u0435 \u2014 \u043d\u0430\u0448 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440 \u0441\u0442\u0440\u0430\u0434\u0430\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 \u044d\u0442\u0430\u043f\u0435 prerender&#8217;\u0430. \u041d\u0430\u0440\u0438\u0441\u043e\u0432\u0430\u0442\u044c \u0443\u0436\u0435 \u0433\u043e\u0442\u043e\u0432\u044b\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u043d\u0435 \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0442\u0440\u0443\u0434\u0430.<\/p>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c &#8212; \u0441\u043a\u0440\u0438\u043d\u0448\u043e\u0442<\/b><\/p>\n<div class=\"spoiler_text\">\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/yy\/t2\/kl\/yyt2kluzxk7lq8hahiysvcxn92c.png\" alt=\"performance\"><\/p>\n<\/div>\n<\/div>\n<p>  <\/p>\n<p><strong>\u041f\u0430\u043c\u044f\u0442\u044c<\/strong><\/p>\n<p>  <\/p>\n<p>\u0414\u043b\u044f \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0430\u043d\u0438\u043c\u0430\u0446\u0438\u0438 \u043a\u0430\u043a \u0443 \u043d\u0430\u0441 \u0432\u0441\u0451 \u044d\u0442\u043e \u0434\u0435\u043b\u043e \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442:<br \/>  ~ 432 \/ 2 = 200 \u043a\u0430\u0434\u0440\u043e\u0432<br \/>  ~ 25 \u043c\u0431<\/p>\n<p>  <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/xo\/pj\/yv\/xopjyv3-myjkr4xupv4_s8kmqeg.png\" alt=\"performance\"><\/p>\n<p>  <\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>  <\/p>\n<p>\u0415\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043f\u043e\u0434\u043e\u0436\u0434\u0430\u0442\u044c, \u043f\u0440\u0435\u0436\u0434\u0435 \u0447\u0435\u043c \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u0430\u043d\u0438\u043c\u0430\u0446\u0438\u044e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e \u0438 \u043e\u043d\u0430 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0441\u044a\u0435\u0434\u0430\u0442\u044c \u0413\u0438\u0433\u0430\u0431\u0430\u0439\u0442\u044b \u043f\u0430\u043c\u044f\u0442\u0438 \u2014 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0439\u0442\u0435 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0447\u0442\u043e \u0442\u043e \u043f\u043e\u0434\u043e\u0431\u043d\u043e\u0435.<\/p>\n<p>  <\/p>\n<h2 id=\"ps\">P.S.<\/h2>\n<p>  <\/p>\n<p>\u0415\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u0438\u0434\u0435\u0438 \u043f\u043e \u043f\u043e\u0432\u044b\u0448\u0435\u043d\u0438\u044e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0430 \u2014 \u043f\u0438\u0448\u0438\u0442\u0435 \ud83d\ude42<\/p>\n<\/div>\n<p> \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\/post\/489602\/\"> https:\/\/habr.com\/ru\/post\/489602\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/489602\/\">\n<p>\u0412 \u043f\u043e\u043f\u044b\u0442\u043a\u0430\u0445 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c 2D \u0430\u043d\u0438\u043c\u0430\u0446\u0438\u044e \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u0443\u044e \u0432 canvas, \u0431\u044b\u043b \u043d\u0430\u0439\u0434\u0435\u043d \u043e\u0434\u0438\u043d \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442.<\/p>\n<p>  <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/tn\/tw\/jl\/tntwjl946vq-09hyhxmyraiffuu.jpeg\" alt=\"hero image\"><\/p>\n<p>  <\/p>\n<p>\u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0432\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u2014 prerender.<\/p>\n<p>  <\/p>\n<p>&quot;\u0410 \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0432\u0441\u0435 \u043a\u0430\u0434\u0440\u044b \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0438 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u0438\u0445 \u043f\u043e\u0441\u043b\u0435 \u043e\u043a\u043e\u043d\u0447\u0430\u043d\u0438\u044f \u0430\u043d\u0438\u043c\u0430\u0446\u0438\u0438?&quot; \u2014 \u043f\u043e\u0434\u0443\u043c\u0430\u043b\u0438 \u043c\u044b \u0441 \u0442\u043e\u0432\u0430\u0440\u0438\u0449\u0435\u043c \u0438 \u0432\u043e\u0442 \u0447\u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0434\u0435\u043d\u044c.<\/p>\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-299221","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/299221","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=299221"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/299221\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=299221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=299221"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=299221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}