{"id":267577,"date":"2015-11-05T20:26:03","date_gmt":"2015-11-05T17:26:03","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=267577"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=267577","title":{"rendered":"100 \u0441\u0442\u0440\u043e\u043a \u043d\u0430 canvas-\u0435: \u0447\u0430\u0441\u0442\u044c 1"},"content":{"rendered":"<p>       <sub><font color=\"#666\"><i>\u041f\u0440\u0435\u0434\u0438\u0441\u043b\u043e\u0432\u0438\u0435\u043c \u043c\u043d\u0435 \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0431\u044b \u043f\u043e\u0437\u0434\u0440\u0430\u0432\u0438\u0442\u044c \u043e\u0434\u043d\u043e\u0433\u043e \u0445\u0430\u0431\u0440\u0430\u044e\u0437\u0435\u0440\u0430 \u0441 \u0434\u043d\u0451\u043c \u0440\u043e\u0436\u0434\u0435\u043d\u0438\u044f. \u0420\u0430\u0441\u0442\u0438 \u0431\u043e\u043b\u044c\u0448\u0438\u043c, \u0431\u0443\u0434\u044c \u0443\u043c\u043d\u044b\u043c, \u0438 \u0434\u043e\u043f\u0438\u043b\u0438 \u0443\u0436\u0435 \u043d\u0430\u043a\u043e\u043d\u0435\u0446 \u0441\u0432\u043e\u0439 canvas-\u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a <a href=\"http:\/\/habrahabr.ru\/post\/239261\/\">Graphics2D<\/a> \u0434\u043e \u0442\u043e\u0433\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0441\u0447\u0438\u0442\u0430\u0435\u0448\u044c \u043f\u0440\u0438\u0435\u043c\u043b\u0435\u043c\u044b\u043c.<br \/>  <b>\u0421 \u0434\u043d\u0451\u043c \u0440\u043e\u0436\u0434\u0435\u043d\u0438\u044f, \u044f.<\/b> \ud83d\ude1b<\/i><\/font><\/sub><\/p>\n<p>  \u042d\u0442\u0438\u043c \u043b\u0435\u0442\u043e\u043c \u043c\u043d\u0435 \u043f\u0440\u0438\u0448\u043b\u0430 \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0430\u044f \u043c\u044b\u0441\u043b\u044c: \u0435\u0441\u043b\u0438 \u0431\u044b \u044f \u043f\u0438\u0441\u0430\u043b \u043c\u0438\u043a\u0440\u043e\u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 \u0434\u043b\u044f canvas \u0432 100 \u0441\u0442\u0440\u043e\u043a, \u0447\u0442\u043e \u0431\u044b \u044f \u0442\u0443\u0434\u0430 \u0443\u043c\u0435\u0441\u0442\u0438\u043b?.. \u0421\u0430\u043c\u044b\u0439 \u0440\u0430\u0437\u0432\u0451\u0440\u043d\u0443\u0442\u044b\u0439 \u043e\u0442\u0432\u0435\u0442 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0437\u0430 1 \u0432\u0435\u0447\u0435\u0440. \u0410 \u043f\u043e\u0442\u043e\u043c \u043f\u0440\u0438\u0448\u043b\u0430 \u0438 \u0438\u0434\u0435\u044f \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438.<\/p>\n<p>  \u041f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u041e\u041e\u041f, \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u0438 \u0430\u043d\u0438\u043c\u0430\u0446\u0438\u044e \u043d\u0430 canvas \u2014 \u0441\u0430\u043c\u044b\u0435 \u0447\u0430\u0441\u0442\u043e \u043d\u0443\u0436\u043d\u044b\u0435 (\u0438\u043c\u0445\u043e) \u0432\u0435\u0449\u0438\u2026 \u0438 \u0432\u0441\u0451 \u044d\u0442\u043e \u0432 100 \u0441\u0442\u0440\u043e\u043a. \u0427\u0430\u0441\u0442\u044c \u043f\u0435\u0440\u0432\u0430\u044f.<br \/>  <a name=\"habracut\"><\/a><br \/>  <font color=\"red\">\u0414\u0438\u0441\u043a\u043b\u0435\u0439\u043c\u0435\u0440:<\/font> \u0442\u0443\u0442 \u0432\u0430\u0441 (\u0438\u043d\u043e\u0433\u0434\u0430) \u043f\u043e\u0434\u0436\u0438\u0434\u0430\u044e\u0442 \u0441\u043e\u0432\u0441\u0435\u043c \u043d\u0435\u043d\u0443\u0436\u043d\u044b\u0435 \u0438\u0437\u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u044d\u043a\u043e\u043d\u043e\u043c\u0438\u0438 \u043f\u0430\u0440\u044b \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 \u043a\u043e\u0434\u0430. \u0410\u0432\u0442\u043e\u0440 (\u0430 \u044d\u0442\u043e \u044f) \u0441\u0447\u0438\u0442\u0430\u0435\u0442, \u0447\u0442\u043e \u0432 \u043c\u0438\u043a\u0440\u043e\u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430\u0445 \u0442\u0430\u043a \u043c\u043e\u0436\u043d\u043e, \u0438 \u043e\u0447\u0435\u043d\u044c \u0447\u0430\u0441\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442\u0441\u044f. \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u043d\u0435 \u043d\u0430\u0440\u0443\u0448\u0430\u0435\u0442 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c, \u043a\u043e\u043d\u0435\u0447\u043d\u043e.<\/p>\n<h4><font color=\"#4682b4\">\u0420\u0430\u0434 \u0432\u0438\u0434\u0435\u0442\u044c \u0432\u0430\u0441 \u043f\u043e\u0434 \u043a\u0430\u0442\u043e\u043c \ud83d\ude09<\/font><\/h4>\n<p>  \u041d\u0430\u0447\u043d\u0451\u043c \u0441 \u0438\u0434\u0435\u0438 (\u0430 \u043f\u0435\u0440\u0432\u044b\u043c \u0434\u0435\u043b\u043e\u043c \u2014 \u041e\u041e\u041f). 3 \u0433\u043b\u0430\u0432\u043d\u044b\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u0430: \u043f\u0443\u0442\u0438, \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f, \u0442\u0435\u043a\u0441\u0442. \u041d\u0435\u0442 \u043d\u0438\u043a\u0430\u043a\u043e\u0439 \u043d\u0443\u0436\u0434\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u0440\u044f\u043c\u043e\u0443\u0433\u043e\u043b\u044c\u043d\u0438\u043a\u0438 \u0438 \u043a\u0440\u0443\u0433\u0438 \u0432 \u043c\u0438\u043d\u0438\u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435: \u043e\u043d\u0438 \u043b\u0435\u0433\u043a\u043e \u0441\u043e\u0437\u0434\u0430\u044e\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 \u043f\u0443\u0442\u044c. \u041a\u0430\u043a \u0438 \u0441\u043f\u0440\u0430\u0439\u0442\u044b \u2014 \u0447\u0435\u0440\u0435\u0437 \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0438. \u0418 \u0442.\u043f.<br \/>  \u041f\u0435\u0440\u0432\u044b\u0439 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u2014 \u0435\u0433\u043e \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435.<br \/>  \u0412\u0442\u043e\u0440\u043e\u0439 \u2014 \u0441\u0442\u0438\u043b\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u043d\u0430 canvas \u043f\u0435\u0440\u0435\u0434 \u0440\u0438\u0441\u043e\u0432\u0430\u043d\u0438\u0435\u043c.<\/p>\n<p>  \u042f \u043d\u0430\u0437\u043e\u0432\u0443 \u044d\u0442\u043e Rat \ud83d\ude1b  <\/p>\n<pre><code class=\"javascript\">Rat = function(context){     this.context = context; }; <\/code><\/pre>\n<h4><font color=\"#4682b4\">\u041f\u0443\u0442\u0438<\/font><\/h4>\n<p>  \u041a\u0430\u043a-\u0442\u043e \u0442\u0430\u043a \u0431\u0443\u0434\u0435\u0442 \u043d\u0435\u043f\u043b\u043e\u0445\u043e:<br \/>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/9b8\/a96\/58f\/9b8a9658fc6d7199be19c9dcd2337a75.png\" align=\"right\"\/>  <\/p>\n<pre><code class=\"javascript\">var path = rat.path([   ['moveTo', 10, 10],   ['lineTo', 100, 100],   ['lineTo', 10, 100],   ['closePath'] ], {   fillStyle: 'red',   strokeStyle: 'green',   lineWidth: 4 }); <\/code><\/pre>\n<p>  \u0423 \u0432\u0441\u0435\u0445 3 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u043d\u0443\u0436\u043d\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442, \u043e\u0431\u044a\u0435\u043a\u0442 \u0434\u043b\u044f \u0441\u0442\u0438\u043b\u0435\u0439 \u0438 \u0442.\u043f\u2026 \u0422\u0430\u043a \u0447\u0442\u043e:  <\/p>\n<pre><code class=\"javascript\">Rat.init = function(cls, arg){     cls.opt = arg[0];     cls.style = arg[1] || {};     cls.context = arg[2];     cls.draw(arg[2].context); }; <\/code><\/pre>\n<p>  \u0412\u0440\u043e\u0434\u0435 \u0431\u044b \u0432\u0441\u0451 \u043f\u043e\u043d\u044f\u0442\u043d\u043e? \u0423 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0435\u0441\u0442\u044c 3 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430: opt (1 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442), style (2\u0439) \u0438 context (\u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442), \u0430 \u0442\u0430\u043a\u0436\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u044f draw(ctx), \u0440\u0438\u0441\u0443\u044e\u0449\u0430\u044f \u044d\u0442\u043e\u0442 \u043e\u0431\u044a\u0435\u043a\u0442.<\/p>\n<p>  \u041d\u0430\u0448 \u043a\u043b\u0430\u0441\u0441:  <\/p>\n<pre><code class=\"javascript\">Rat.Path = function(opt, style, context){     Rat.init(this, arguments); }; <\/code><\/pre>\n<p>  \u0414\u0430, \u043a\u0430\u043a \u043d\u0438 \u0441\u0442\u0440\u0430\u043d\u043d\u043e, \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u2014 \u0432\u0441\u0451.<\/p>\n<p>  \u0421\u0430\u043c\u043e\u0435 \u0433\u043b\u0430\u0432\u043d\u043e\u0435: \u043e\u0442\u0440\u0438\u0441\u043e\u0432\u043a\u0430:  <\/p>\n<pre><code class=\"javascript\">Rat.Path.prototype = {     draw: function(ctx){         this.process(function(ctx){             if(this.style.fillStyle)                 ctx.fill();             if(this.style.strokeStyle)                 ctx.stroke();         }, ctx);     },     process: function(callback, ctx){         ctx = ctx || this.context.context;         Rat.style(ctx, this.style);         ctx.beginPath();         this.opt.forEach(function(func){             ctx[func[0]].apply(ctx, func.slice(1));         });         var result = callback.call(this, ctx);         ctx.restore();         return result;     } }; <\/code><\/pre>\n<p>  \u0424\u0443\u043d\u043a\u0446\u0438\u044f process \u0442\u0443\u0442 \u0432\u043e\u0432\u0441\u0435 \u043d\u0435\u0441\u043f\u0440\u043e\u0441\u0442\u0430: \u043e\u043d\u0430 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u0435\u0449\u0451 \u043a\u043e\u0435-\u0433\u0434\u0435:  <\/p>\n<pre><code class=\"javascript\">    isPointIn: function(x,y, ctx){         return this.process(function(ctx){ return ctx.isPointInPath(x, y); }, ctx);     } <\/code><\/pre>\n<p>  \u0417\u0430\u0447\u0435\u043c callback? \u0425\u043c\u2026 \u0414\u043b\u044f \u043a\u0440\u0430\u0441\u043e\u0442\u044b.<\/p>\n<p>  \u0424\u0443\u043d\u043a\u0446\u0438\u044f <b>Rat.style<\/b>, \u0442\u0430\u043a\u0436\u0435 \u043e\u0431\u0449\u0430\u044f \u0434\u043b\u044f \u0432\u0441\u0435\u0445 3 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432, \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u0438\u0442 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 \u043d\u0430 canvas. \u041d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0435\u043c, \u0447\u0442\u043e \u043d\u0430\u043c \u0442\u0430\u043a\u0436\u0435 \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0439:  <\/p>\n<pre><code class=\"javascript\">\/\/ \u043d\u0435 \u0441\u043c\u043e\u0442\u0440\u0438\u0442\u0435 \u043d\u0430 \u043c\u0435\u043d\u044f \u0442\u0430\u043a, \u0432 \u043c\u0438\u043a\u0440\u043e\u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430\u0445 \u0438\u043d\u043e\u0433\u0434\u0430 \u043c\u043e\u0436\u043d\u043e \u0442\u0430\u043a \u0438\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \/\/ \u0438\u043d\u043e\u0433\u0434\u0430 Rat.notStyle = &quot;translate0rotate0transform0scale&quot;.split(0); Rat.style = function(ctx, style){     ctx.save();     style.origin && ctx.translate.apply(ctx, style.origin);     style.rotate && ctx.rotate(style.rotate);     style.scale && ctx.scale.apply(ctx, style.scale);     style.origin && ctx.translate(-style.origin[0], -style.origin[1]);     style.translate && ctx.translate.apply(ctx, style.translate); \/\/ \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e, \u044d\u0442\u043e \u043b\u0443\u0447\u0448\u0435 \u0434\u043e \u0438\u043b\u0438 \u043f\u043e\u0441\u043b\u0435 origin?     style.transform && ctx.transform.apply(ctx, style.transform);     Object.keys(style).forEach(function(key){         if(!~Rat.notStyle.indexOf(key))             ctx[key] = style[key];     }); }; <\/code><\/pre>\n<p>  \u0410\u0439, \u043d\u0435 \u0431\u0435\u0439\u0442\u0435, \u044f \u0432\u0441\u0435 \u043e\u0431\u044a\u044f\u0441\u043d\u044e. <code>!~Rat.notStyle.indexOf(key)<\/code> \u2014 \u0442\u043e\u0436\u0435 \u0441\u0430\u043c\u043e\u0435, \u0447\u0442\u043e \u0438 <code>Rat.notStyle.indexOf(key) != -1<\/code>. \u042d\u0442\u043e \u043c\u0438\u043a\u0440\u043e\u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0432\u0441\u0451 \u0436\u0435.<\/p>\n<p>  \u041d\u0443 \u0438, \u043d\u0430\u043a\u043e\u043d\u0435\u0446, \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430, \u0441\u043e\u0437\u0434\u0430\u044e\u0449\u0430\u044f \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0449\u0430\u044f \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u043d\u0430\u0448\u0435\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430:  <\/p>\n<pre><code class=\"javascript\">Rat.prototype = {     path : function(opt, style){ return new Rat.Path(opt, style, this); }, }; <\/code><\/pre>\n<p>  \u0412\u0441\u0451, \u043c\u043e\u0436\u043d\u043e \u0440\u0438\u0441\u043e\u0432\u0430\u0442\u044c \u043f\u0443\u0442\u0438. \u0423\u0440\u0430!<\/p>\n<p>  \u0418, \u043f\u043e\u043c\u0438\u043c\u043e \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u0441\u0442\u0438\u043b\u0435\u0439, \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u044e\u0442, \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c \u0432 Rat.style, \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438:<br \/>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/cc8\/6bd\/f18\/cc86bdf18c959c0025b7a8a5460315d3.png\" align=\"right\"\/>  <\/p>\n<pre><code class=\"javascript\">var path = rat.path([   ['moveTo', 10, 10],   ['lineTo', 100, 100],   ['lineTo', 10, 100],   ['closePath'] ], {   fillStyle: 'red',   strokeStyle: 'green',   lineWidth: 4,   rotate: 45 \/ 180 * Math.PI,   origin: [55, 55] }); <\/code><\/pre>\n<p><sup><i>\u041a\u0430\u0440\u0442\u0438\u043d\u043a\u0430 \u043e\u0431\u0440\u0435\u0437\u0430\u043d\u0430, \u0442.\u043a. \u043d\u0430\u0440\u0438\u0441\u043e\u0432\u0430\u043d\u0430 \u0432 \u043d\u0443\u043b\u0435\u0432\u044b\u0445 \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442\u0430\u0445.<\/i><\/sup><\/p>\n<h4><font color=\"#4682b4\">\u041a\u0430\u0440\u0442\u0438\u043d\u043a\u0438<\/font><\/h4>\n<p>  \u0421\u043b\u0435\u0434\u0443\u044f \u0434\u0430\u043b\u044c\u0448\u0435 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0443 2 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432, \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0432\u043e\u043e\u0442 \u0442\u0430\u043a\u043e\u0439 \u0432\u043e\u0442 \u043a\u043b\u0430\u0441\u0441:  <\/p>\n<pre><code class=\"javascript\">var img = new Image(); img.src = &quot;image.jpg&quot;; img.onload = function(){   rat.image(img); } <\/code><\/pre>\n<p>  \u041f\u043e\u043c\u0438\u043c\u043e \u044d\u0442\u043e\u0433\u043e, \u0432 \u0441\u0442\u0438\u043b\u044f\u0445 \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b <b>width<\/b>, <b>height<\/b> \u0438 <b>crop<\/b> (\u043c\u0430\u0441\u0441\u0438\u0432 \u0438\u0437 4 \u0447\u0438\u0441\u0435\u043b). \u0412\u0441\u0451 \u0442\u0430\u043a \u0436\u0435, \u043a\u0430\u043a \u0432 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0439 <u>drawImage<\/u> CanvasRendering2DContext-\u0430.<\/p>\n<p>  \u0421\u043d\u043e\u0432\u0430 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u043a\u043b\u0430\u0441\u0441\u0430:  <\/p>\n<pre><code class=\"javascript\">Rat.Image = function(opt, style, context){     Rat.init(this, arguments); }; <\/code><\/pre>\n<p>  \u041e\u0442\u0440\u0438\u0441\u043e\u0432\u043a\u0430 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043a\u0430\u043a-\u0442\u043e \u0442\u0430\u043a:  <\/p>\n<pre><code class=\"javascript\">Rat.Image.prototype.draw = function(ctx){     Rat.style(ctx, this.style);     if(this.style.crop)         ctx.drawImage.apply(ctx, [this.opt, 0, 0].concat(this.style.crop));     else         ctx.drawImage(this.opt, 0, 0, this.style.width || this.opt.width, this.style.height || this.opt.height);     ctx.restore(); }; <\/code><\/pre>\n<p>  \u0412\u0441\u0451, \u0432\u0440\u043e\u0434\u0435 \u0431\u044b, \u043f\u0440\u043e\u0441\u0442\u043e.<\/p>\n<p>  \u0418 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435, \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435:  <\/p>\n<pre><code class=\"javascript\">Rat.prototype = {     ...     image : function(opt, style){ return new Rat.Image(opt, style, this); }, }; <\/code><\/pre>\n<p>  \u0423\u0440\u0430, \u0438 \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0438 \u0435\u0441\u0442\u044c.<\/p>\n<h4><font color=\"#4682b4\">\u0422\u0435\u043a\u0441\u0442<\/font><\/h4>\n<p>  3\u0439 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442:  <\/p>\n<pre><code class=\"javascript\">var text = rat.text(&quot;Hello, world!&quot;, {   fillStyle: 'blue' }); <\/code><\/pre>\n<p><sub>\u0422\u0430\u043a\u0436\u0435 \u0435\u0441\u0442\u044c \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e maxWidth.<\/sub><\/p>\n<p>  \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440:  <\/p>\n<pre><code class=\"javascript\">Rat.Text = function(){     Rat.init(this, arguments); }; <\/code><\/pre>\n<p>  \u041e\u0442\u0440\u0438\u0441\u043e\u0432\u043a\u0430 \u043e\u0447\u0435\u043d\u044c \u043f\u0440\u043e\u0441\u0442\u0430\u044f. \u0410 \u0440\u0435\u0448\u0435\u043d\u0438\u0435, \u043a\u0430\u043a \u0432\u0441\u0435\u0433\u0434\u0430, \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u0447\u0438\u0441\u0442\u043e\u0435, \u0437\u0430\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0449\u0435\u0435 ).  <\/p>\n<pre><code class=\"javascript\">Rat.Text.prototype.draw = function(ctx){     Rat.style(ctx, this.style);     if(this.style.fillStyle)         ctx.fillText(this.opt, 0, 0, this.style.maxWidth || 999999999999999);     if(this.style.strokeStyle)         ctx.strokeText(this.opt, 0, 0, this.style.maxWidth || 9999999999999999);     ctx.restore(); }; <\/code><\/pre>\n<p>  \u0410 \u0435\u0449\u0451 \u0442\u0435\u043a\u0441\u0442 \u043d\u0430 canvas-\u0435 \u043c\u043e\u0436\u043d\u043e \u043c\u0435\u0440\u0438\u0442\u044c. \u0428\u0438\u0440\u0438\u043d\u0443, \u0434\u0430. \u0412\u044b\u0441\u043e\u0442\u0430 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u043c \u0448\u0440\u0438\u0444\u0442\u0430.  <\/p>\n<pre><code class=\"javascript\">Rat.Text.prototype.measure = function(){     var ctx = this.context.context;     Rat.style(ctx, this.style);     var w = ctx.measureText(this.opt).width;     ctx.restore();     return w; }; <\/code><\/pre>\n<p>  \u041d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0435\u043c:  <\/p>\n<pre><code class=\"javascript\">Rat.prototype = {     ...     image : function(opt, style){ return new Rat.Image(opt, style, this); }, }; <\/code><\/pre>\n<h4><font color=\"#4682b4\">\u041f\u043e \u043c\u0435\u043b\u043e\u0447\u0438<\/font><\/h4>\n<p>  \u0418\u043d\u043e\u0433\u0434\u0430 \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c, \u0437\u0430\u0431\u044b\u0442\u044c, \u0432\u044b\u043a\u0438\u043d\u0443\u0442\u044c \u0432\u0441\u0451 \u0438 \u043d\u0430\u0447\u0430\u0442\u044c \u0441 \u0447\u0438\u0441\u0442\u043e\u0433\u043e \u043b\u0438\u0441\u0442\u0430. \u0414\u043b\u044f \u0442\u0430\u043a\u0438\u0445 \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u0435\u0441\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044f clear:  <\/p>\n<pre><code class=\"javascript\">Rat.prototype = { ...     clear: function(){         var cnv = this.context.canvas;         this.context.clearRect(0, 0, cnv.width, cnv.height);     } }; <\/code><\/pre>\n<p>  \u0414\u043b\u044f \u0432\u0441\u0435\u0433\u043e \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0435\u0441\u0442\u044c <b>draw<\/b>, \u0440\u0438\u0441\u0443\u044e\u0449\u0438\u0439 \u0432\u0441\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0438\u0437 \u043c\u0430\u0441\u0441\u0438\u0432\u0430:  <\/p>\n<pre><code class=\"javascript\">Rat.prototype = { ...     draw: function(elements){         var ctx = this.context;         elements.forEach(function(element){             element.draw(ctx);         });     } }; <\/code><\/pre>\n<h4><font color=\"#4682b4\">\u041f\u0440\u0438\u043c\u0435\u0440\u044b:<\/font><\/h4>\n<p>  \u041d\u0443 \u0430 \u0442\u0435\u043f\u0435\u0440\u044c\u2026 \u0414\u0430\u0432\u0430\u0439\u0442\u0435, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043d\u0430\u043a\u043e\u0434\u0438\u043c \u043a\u043d\u043e\u043f\u043a\u0443 \u043d\u0430 canvas-\u0435 (\u0441\u0430\u043c\u043e\u0435 \u043f\u0440\u043e\u0441\u0442\u043e\u0435, \u0447\u0442\u043e \u043f\u0440\u0438\u0434\u0443\u043c\u0430\u043b\u043e\u0441\u044c):  <\/p>\n<pre><code class=\"javascript\">\/\/ \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u043a var path = rat.path([ \t['moveTo', 10, 10], \t['lineTo', 100, 10], \t['lineTo', 100, 40], \t['lineTo', 10, 40], \t['closePath'] ], { \tfillStyle: '#eee', \tstrokeStyle: '#aaa', \tlineWidth: 2 });  \/\/ \u0442\u0435\u043a\u0441\u0442 var text = rat.text(&quot;Hello, world&quot;, { \ttranslate: [55, 28], \ttextAlign: 'center', \tfillStyle: 'black' }); <\/code><\/pre>\n<p>  <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/171\/a7b\/aaf\/171a7baaf5200844c1667fcbe04a7a71.png\" width=\"117\" height=\"52\"\/><\/p>\n<p>  \u0418 \u043f\u0443\u0443\u0441\u0442\u044c\u2026 \u041f\u0440\u0438 \u043d\u0430\u0432\u0435\u0434\u0435\u043d\u0438\u0438 \u043c\u044b\u0448\u0438 \u043e\u043d\u0430 \u043f\u043e\u0434\u0441\u0432\u0435\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f:  <\/p>\n<pre><code class=\"javascript\">var bounds = ctx.canvas.getBoundingClientRect(); var hover = false; ctx.canvas.addEventListener('mousemove', function(e){ \tvar x = e.clientX - bounds.left, \t\ty = e.clientY - bounds.top; \tif(x &gt; 10 && x &lt; 100 && y &gt; 10 && y &lt; 40){ \t\tif(hover) \t\t\treturn; \t\thover = true; \t\tpath.style.fillStyle = '#ccc'; \t\trat.clear(); \t\trat.draw([path, text]); \t} \telse if(hover){ \t\thover = false; \t\tpath.style.fillStyle = '#eee'; \t\trat.clear(); \t\trat.draw([path, text]); \t} }); <\/code><\/pre>\n<p>  <img decoding=\"async\" src=\"http:\/\/i.imgur.com\/x4Qc2DY.png\"\/><\/p>\n<h4><font color=\"#4682b4\">\u0410 \u0437\u0430\u0447\u0435\u043c?<\/font><\/h4>\n<p>  \u0421\u0430\u043c\u043e\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0435, \u0447\u0442\u043e \u043d\u0430 \u0431\u0430\u0437\u043e\u0432\u043e\u043c canvas \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u043a\u043e\u0434\u0438\u0442\u044c \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u043e \u0436\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0435\u043c \u0436\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e\u043c \u043a\u043e\u0434\u0430.  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0421\u043a\u0440\u044b\u0442\u044b\u0439 \u0442\u0435\u043a\u0441\u0442<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"javascript\">\/\/ \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u043a var path = { \tfill: '#eee', \tdraw: function(){ \t\tctx.moveTo(10, 10); \t\tctx.lineTo(100, 10); \t\tctx.lineTo(100, 40); \t\tctx.lineTo(10, 40); \t\tctx.closePath();  \t\tctx.fillStyle = this.fill; \t\tctx.strokeStyle = '#aaa'; \t\tctx.lineWidth = 2; \t\tctx.fill(); \t\tctx.stroke(); \t} }; \/\/ \u0442\u0435\u043a\u0441\u0442 var text = { \tdraw: function(){ \t\tctx.textAlign = 'center'; \t\tctx.fillStyle = 'black'; \t\tctx.fillText(&quot;Hello, world&quot;,  55, 28); \t} }; path.draw(); text.draw();  var bounds = ctx.canvas.getBoundingClientRect(); var hover = false; ctx.canvas.addEventListener('mousemove', function(e){ \tvar x = e.clientX - bounds.left, \t\ty = e.clientY - bounds.top; \tif(x &gt; 10 && x &lt; 100 && y &gt; 10 && y &lt; 40){ \t\tif(hover) \t\t\treturn; \t\thover = true; \t\tpath.fill = '#ccc'; \t\tctx.clearRect(0, 0, 800, 400); \t\tpath.draw(); \t\ttext.draw(); \t} \telse if(hover){ \t\thover = false; \t\tpath.fill = '#eee'; \t\tctx.clearRect(0, 0, 800, 400); \t\tpath.draw(); \t\ttext.draw(); \t} }); <\/code><\/pre>\n<\/div>\n<\/div>\n<p>  \u041d\u043e \u044d\u0442\u043e \u0441\u0442\u0430\u043b\u043e \u0432\u0438\u0434\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a 100 \u0441\u0442\u0440\u043e\u043a \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u044b\u2026<br \/>  <a href=\"https:\/\/github.com\/keyten\/Rat.js\/blob\/master\/rat.js\">github.com\/keyten\/Rat.js\/blob\/master\/rat.js<\/a><\/p>\n<p>  \u041d\u0443 \u0447\u0442\u043e \u0436\u2026 \u0412 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0447\u0430\u0441\u0442\u0438 (\u0435\u0441\u043b\u0438 \u0445\u0430\u0431\u0440\u0430\u0445\u0430\u0431\u0440\u0443 \u0431\u0443\u0434\u0435\u0442 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0430 \u044d\u0442\u0430 \u0442\u0435\u043c\u0430) \u044f \u043f\u043e\u043a\u0430\u0436\u0443 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u043c\u044b\u0448\u0438, \u0438 3 \u0447\u0430\u0441\u0442\u044c \u2014 \u0430\u043d\u0438\u043c\u0430\u0446\u0438\u044f. \u0412\u0441\u0451 \u0441\u043d\u043e\u0432\u0430 \u0432 100 \u0441\u0442\u0440\u043e\u043a (\u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f \u043b\u0438 :)).<br \/>  \u041f\u043e\u0439\u0434\u0443 \u043f\u0440\u0430\u0437\u0434\u043d\u043e\u0432\u0430\u0442\u044c \u0434\u0435\u043d\u044c \u0440\u043e\u0436\u0434\u0435\u043d\u0438\u044f.<\/p>\n<h4><font color=\"#4682b4\">\u0412\u0441\u0435\u043c \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430!)<\/font><\/h4>\n<div class=\"clear\"><\/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=\"http:\/\/habrahabr.ru\/post\/270255\/\"> http:\/\/habrahabr.ru\/post\/270255\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>       <sub><font color=\"#666\"><i>\u041f\u0440\u0435\u0434\u0438\u0441\u043b\u043e\u0432\u0438\u0435\u043c \u043c\u043d\u0435 \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0431\u044b \u043f\u043e\u0437\u0434\u0440\u0430\u0432\u0438\u0442\u044c \u043e\u0434\u043d\u043e\u0433\u043e \u0445\u0430\u0431\u0440\u0430\u044e\u0437\u0435\u0440\u0430 \u0441 \u0434\u043d\u0451\u043c \u0440\u043e\u0436\u0434\u0435\u043d\u0438\u044f. \u0420\u0430\u0441\u0442\u0438 \u0431\u043e\u043b\u044c\u0448\u0438\u043c, \u0431\u0443\u0434\u044c \u0443\u043c\u043d\u044b\u043c, \u0438 \u0434\u043e\u043f\u0438\u043b\u0438 \u0443\u0436\u0435 \u043d\u0430\u043a\u043e\u043d\u0435\u0446 \u0441\u0432\u043e\u0439 canvas-\u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a <a href=\"http:\/\/habrahabr.ru\/post\/239261\/\">Graphics2D<\/a> \u0434\u043e \u0442\u043e\u0433\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0441\u0447\u0438\u0442\u0430\u0435\u0448\u044c \u043f\u0440\u0438\u0435\u043c\u043b\u0435\u043c\u044b\u043c.<br \/>  <b>\u0421 \u0434\u043d\u0451\u043c \u0440\u043e\u0436\u0434\u0435\u043d\u0438\u044f, \u044f.<\/b> \ud83d\ude1b<\/i><\/font><\/sub><\/p>\n<p>  \u042d\u0442\u0438\u043c \u043b\u0435\u0442\u043e\u043c \u043c\u043d\u0435 \u043f\u0440\u0438\u0448\u043b\u0430 \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0430\u044f \u043c\u044b\u0441\u043b\u044c: \u0435\u0441\u043b\u0438 \u0431\u044b \u044f \u043f\u0438\u0441\u0430\u043b \u043c\u0438\u043a\u0440\u043e\u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 \u0434\u043b\u044f canvas \u0432 100 \u0441\u0442\u0440\u043e\u043a, \u0447\u0442\u043e \u0431\u044b \u044f \u0442\u0443\u0434\u0430 \u0443\u043c\u0435\u0441\u0442\u0438\u043b?.. \u0421\u0430\u043c\u044b\u0439 \u0440\u0430\u0437\u0432\u0451\u0440\u043d\u0443\u0442\u044b\u0439 \u043e\u0442\u0432\u0435\u0442 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0437\u0430 1 \u0432\u0435\u0447\u0435\u0440. \u0410 \u043f\u043e\u0442\u043e\u043c \u043f\u0440\u0438\u0448\u043b\u0430 \u0438 \u0438\u0434\u0435\u044f \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438.<\/p>\n<p>  \u041f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u041e\u041e\u041f, \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u0438 \u0430\u043d\u0438\u043c\u0430\u0446\u0438\u044e \u043d\u0430 canvas \u2014 \u0441\u0430\u043c\u044b\u0435 \u0447\u0430\u0441\u0442\u043e \u043d\u0443\u0436\u043d\u044b\u0435 (\u0438\u043c\u0445\u043e) \u0432\u0435\u0449\u0438\u2026 \u0438 \u0432\u0441\u0451 \u044d\u0442\u043e \u0432 100 \u0441\u0442\u0440\u043e\u043a. \u0427\u0430\u0441\u0442\u044c \u043f\u0435\u0440\u0432\u0430\u044f.  <\/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-267577","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/267577","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=267577"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/267577\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=267577"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=267577"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=267577"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}