diff --git a/api/README.md b/api/README.md index 2da7b16..aaabdb1 100644 --- a/api/README.md +++ b/api/README.md @@ -1,8 +1,9 @@ -## g2([opts]) +## g2([opts]) ⇒ object Create a queue of 2D graphics commands. **Kind**: global function +**Returns**: object - g2 | Param | Type | Description | | --- | --- | --- | @@ -13,7 +14,7 @@ Create a queue of 2D graphics commands. // How to use g2() var ctx = document.getElementById("c").getContext("2d"); g2() // Create 'g2' instance. .lin(50,50,100,100) // Append ... .lin(100,100,200,50) // ... commands. .exe(ctx); // Execute commands addressing canvas context. ``` -* [g2([opts])](#g2) +* [g2([opts])](#g2) ⇒ object * _instance_ * [.cartesian([on])](#g2+cartesian) ⇒ object * [.pan(dx, dy)](#g2+pan) ⇒ object diff --git a/g2.js b/g2.js index 121c231..7e53b95 100644 --- a/g2.js +++ b/g2.js @@ -1,7 +1,8 @@ /** * g2 (c) 2013-16 Stefan Goessner - * @license - * MIT License + * @license MIT License + * @link https://github.com/goessner/g2 + * */ /* jshint -W014 */ /* jshint -W030 */ @@ -9,6 +10,7 @@ /** * Create a queue of 2D graphics commands. * @param {object} [opts] Custom options object. It is simply copied into the 'g2' object for later individual use. + * @returns {object} g2 * @example * // How to use g2() * var ctx = document.getElementById("c").getContext("2d"); diff --git a/g2.min.js b/g2.min.js index 0b0b5d8..25a152d 100644 --- a/g2.min.js +++ b/g2.min.js @@ -1,6 +1,7 @@ /** * g2 (c) 2013-16 Stefan Goessner - * @license - * MIT License + * @license MIT License + * @link https://github.com/goessner/g2 + * */ function g2(){if(this instanceof g2){if(arguments)Object.assign(this,arguments);this.cmds=[];this.curIdx=false;return this}else return g2.apply(Object.create(g2.prototype),arguments)}g2.prototype.addCmd=function(t){this.cmds.push("proto"in t.c?Object.assign(Object.create(t.c.proto),{g2:this},t):t);return this};Object.defineProperty(g2.prototype,"curCmd",{get:function(){return this.curIdx!==false?this.cmds[this.curIdx]:null}});Object.defineProperty(g2.prototype,"state",{get:function(){return this._state||(this._state=g2.State.create(this))}});g2.prototype.cartesian=function t(e){this.state.cartesian=e!==false;return this};g2.prototype.pan=function e(t,i){this.state.trf0.x+=t;this.state.trf0.y+=i;return this};g2.prototype.zoom=function i(t,e,s){this.state.trf0.x=(1-t)*(e||0)+t*this.state.trf0.x;this.state.trf0.y=(1-t)*(s||0)+t*this.state.trf0.y;this.state.trf0.scl*=t;return this};g2.prototype.view=function s(t,e,i){this.state.trf0.x=t;this.state.trf0.y=e;this.state.trf0.scl=i;return this};g2.prototype.del=function r(){this.cmds.length=0;return this};g2.prototype._curPnt=function(){var t=this.cmds.length&&this.cmds[this.cmds.length-1]||false;return t&&(t.cp||t.a)};g2.prototype.p=function n(){return this.addCmd({c:n})};g2.prototype.m=function o(t,e){return this.addCmd({c:o,a:[t,e]})};g2.prototype.l=function a(t,e){return this.addCmd({c:a,a:[t,e]})};g2.prototype.q=function c(t,e,i,s){return this.addCmd({c:c,a:[t,e,i,s],cp:[i,s]})};g2.prototype.c=function h(t,e,i,s,r,n){return this.addCmd({c:h,a:[t,e,i,s,r,n],cp:[r,n]})};g2.prototype.a=function f(t,e,i){var s=this._curPnt(),r=2*Math.PI;if(s&&(t>Number.EPSILON&&t-r)){var n=e-s[0],o=i-s[1],a=Math.tan(t/2),c=n/2-o/a/2,h=o/2+n/a/2,d=Math.atan2(-h,-c);return this.addCmd({c:f,a:[s[0]+c,s[1]+h,Math.hypot(c,h),d,d+t,t<0],cp:[e,i]})}else return this.addCmd({c:g2.prototype.l,a:[e,i]})};g2.prototype.z=function d(){return this.addCmd({c:d})};g2.prototype.stroke=function p(t,e){var i=t===undefined&&e===undefined?[]:typeof t==="string"?[null,t]:typeof t==="object"&&!e?[t]:[t,e];return this.addCmd({c:p,a:i})};g2.prototype.fill=function l(t,e){var i=t===undefined&&e===undefined?[]:typeof t==="string"?[null,t]:typeof t==="object"&&!e?[t]:[t,e];return this.addCmd({c:l,a:i})};g2.prototype.drw=function u(t,e){var i=t===undefined&&e===undefined?[]:typeof t==="string"?[null,t]:typeof t==="object"&&!e?[t]:[t,e];return this.addCmd({c:u,a:i})};g2.prototype.txt=function g(t,e,i,s,r){return this.addCmd({c:g,a:[t,e||0,i||0,s||0,r]})};g2.prototype.img=function y(t,e,i,s,r,n,o,a,c){var h=new Image,f=this.state;f.loading++;h.onload=function d(){f.loaded()};h.onerror=function(){h.src=g2.prototype.img.broken};h.src=t;return this.addCmd({c:y,a:[h,e+.5||0,i+.5||0,s,r,n,o,a,c]})};g2.prototype.img.broken="data:image/gif;base64,R0lGODlhHgAeAKIAAAAAmWZmmZnM/////8zMzGZmZgAAAAAAACwAAAAAHgAeAEADimi63P5ryAmEqHfqPRWfRQF+nEeeqImum0oJQxUThGaQ7hSs95ezvB4Q+BvihBSAclk6fgKiAkE0kE6RNqwkUBtMa1OpVlI0lsbmFjrdWbMH5Tdcu6wbf7J8YM9H4y0YAE0+dHVKIV0Efm5VGiEpY1A0UVMSBYtPGl1eNZhnEBGEck6jZ6WfoKmgCQA7";g2.prototype.lin=function m(t,e,i,s,r){return this.addCmd({c:m,a:[t,e,i,s,r]})};g2.prototype.rec=function v(t,e,i,s,r){return this.addCmd({c:v,a:[t,e,i,s,r]})};g2.prototype.cir=function x(t,e,i,s){return this.addCmd({c:x,a:[t,e,i,s]})};g2.prototype.arc=function b(t,e,i,s,r,n){return this.addCmd({c:b,a:[t,e,i,s||0,r||2*Math.PI,n]})};g2.prototype.ply=function C(t,e,i){var s=C.itrOf(t,i);if(s)this.addCmd({c:C,a:[t,e,s,i]});return this};g2.prototype.ply.iterators={"x,y":function(t,e){return e=2?g2.prototype.ply.iterators["[x,y]"]:typeof t[0]==="object"&&"x"in t[0]&&"y"in t[0]?g2.prototype.ply.iterators["{x,y}"]:undefined};g2.prototype.beg=function j(t){return this.addCmd(t?{c:j,a:[t],open:true}:{c:j,open:true})};g2.prototype.end=function S(){return this.addCmd({c:S})};g2.prototype.end.myBeg=function(t){if(t.c===g2.prototype.beg.cmd&&t.open===true){delete t.open;return true}return false};g2.prototype.clr=function k(){return this.addCmd({c:k})};g2.prototype.grid=function A(t,e){this.state.gridBase=2;this.state.gridExp=1;return this.addCmd({c:A,a:[t,e]})};g2.prototype.grid.getSize=function(t,e){var i=t.gridBase||2,s=t.gridExp||1,r;while((r=e*i*Math.pow(10,s))<14||r>35){if(r<14){if(i==1)i=2;else if(i==2)i=5;else if(i==5){i=1;s++}}else{if(i==1){i=5;s--}else if(i==2)i=1;else if(i==5)i=2}}t.gridBase=i;t.gridExp=s;return r};g2.prototype.use=function O(t,e){if(typeof t==="string")t=g2.symbol[t];if(t&&t!==this){var i=this.state;if(t.state&&t.state.loading){i.loading++;t.state.onload(g2.State.prototype.loaded.bind(i))}this.addCmd({c:O,a:[t,e]})}return this};g2.prototype.style=function P(t){return this.addCmd({c:P,a:[t]})};g2.prototype.exe=function T(t,e){var i=g2.ifcof(t);if(i){var s=(e||this).cmds;if(this.state&&this.state.loading){requestAnimationFrame(T.bind(this,t,e))}else if(t&&s){var r=g2.context[i](t),n=e&&e.state;T.pre[i].call(r,this);if(e)e.state=this.state;for(var o=0,a=s.length,c;o=0;e--)if(t(this.cmds[e],e,this.cmds))return e;return false};g2.prototype.pntToUsr=function _(t,e,i){var s=this.state&&this.state.trf0||false;return!s?{x:t,y:e}:this.state.cartesian?{x:(t-s.x)/s.scl,y:-(e-(i-s.y))/s.scl}:{x:(t-s.x)/s.scl,y:(e-s.y)/s.scl}};g2.prototype.vecToUsr=function E(t,e){var i=this.state&&this.state.trf0||false;return!i?{x:t,y:e}:this.state.cartesian?{x:t/i.scl,y:-e/i.scl}:{x:t/i.scl,y:e/i.scl}};g2.prototype.dump=function(t){function e(t){var i=[],s,r,n,o,a;for(var c=0,h=t.cmds.length;c1){this.shadowOffsetX=t[0];this.shadowOffsetY=t[1];this.shadowBlur=t[2]||0;this.shadowColor=t[3]||"rgba(0,0,0,0.5)"}},thal:function(t){this.textAlign=t},tval:function(t){this.textBaseline=t},fos:function(t,e){this.font=e.cssFont},fow:function(t,e){this.font=e.cssFont},foz:function(t,e){this.font=e.cssFont},fof:function(t,e){this.font=e.cssFont},trf:function(t,e){var i=t.scl||1,s=i*(t.w?Math.sin(t.w):0),r=i*(t.w?Math.cos(t.w):1);this.transform(r,s,-s,r,t.x||0,t.y||0);if(e.get("lwnosc")&&i!==1)g2.State.c2d.lwscale.call(this,1/i)},matrix:function(t){this.transform.apply(this,t)},lwnosc:function(t,e){var i=e.trf.scl;if(i!==1)g2.State.c2d.lwscale.call(this,t?1/i:i)},texture:function X(t){return t.indexof&&(t.indexOf("hatch(")===0?g2.State.c2d.hatch.call(this,t.substring(6,t.indexOf(")",7)).split(",")):t==="transparent"?g2.transparent:t)||t},lwscale:function(t){var e=this.getLineDash();this.lineWidth*=t;if(e.length){for(var i=0,s=e.length;i