diff --git a/dist/core.js b/dist/core.js index e45a4be5..d7989ef1 100644 --- a/dist/core.js +++ b/dist/core.js @@ -1 +1 @@ -kontra={init(t){var n=this.canvas=document.getElementById(t)||t||document.querySelector("canvas");this.context=n.getContext("2d"),this.context.imageSmoothingEnabled=!1,this._init()},_noop:new Function,_tick:new Function,_init:new Function}; \ No newline at end of file +!function(){let t={};window.kontra={init(t){let n=this.canvas=document.getElementById(t)||t||document.querySelector("canvas");this.context=n.getContext("2d"),this.context.imageSmoothingEnabled=!1,this.emit("init",this)},on(n,e){t[n]=t[n]||[],t[n].push(e)},off(n,e,i){!t[n]||(i=t[n].indexOf(e))<0||t[n].splice(i,1)},emit(n,...e){t[n]&&t[n].forEach(t=>t(...e))},_noop:new Function,_callbacks:t}}(); \ No newline at end of file diff --git a/dist/gameLoop.js b/dist/gameLoop.js index 10aaeff2..05c3f3ab 100644 --- a/dist/gameLoop.js +++ b/dist/gameLoop.js @@ -1 +1 @@ -kontra.gameLoop=function(e){let t,n,a,r,o=(e=e||{}).fps||60,i=0,p=1e3/o,c=1/o,s=!1===e.clearCanvas?kontra._noop:function(){kontra.context.clearRect(0,0,kontra.canvas.width,kontra.canvas.height)};function d(){if(n=requestAnimationFrame(d),a=performance.now(),r=a-t,t=a,!(r>1e3)){for(kontra._tick(),i+=r;i>=p;)m.update(c),i-=p;s(),m.render()}}let m={update:e.update,render:e.render,isStopped:!0,start(){t=performance.now(),this.isStopped=!1,requestAnimationFrame(d)},stop(){this.isStopped=!0,cancelAnimationFrame(n)}};return m}; \ No newline at end of file +kontra.gameLoop=function(e){let t,n,a,r,o=(e=e||{}).fps||60,i=0,p=1e3/o,c=1/o,s=!1===e.clearCanvas?kontra._noop:function(){kontra.context.clearRect(0,0,kontra.canvas.width,kontra.canvas.height)};function d(){if(n=requestAnimationFrame(d),a=performance.now(),r=a-t,t=a,!(r>1e3)){for(kontra.emit("tick"),i+=r;i>=p;)m.update(c),i-=p;s(),m.render()}}let m={update:e.update,render:e.render,isStopped:!0,start(){t=performance.now(),this.isStopped=!1,requestAnimationFrame(d)},stop(){this.isStopped=!0,cancelAnimationFrame(n)}};return m}; \ No newline at end of file diff --git a/dist/plugin.js b/dist/plugin.js new file mode 100644 index 00000000..722fd293 --- /dev/null +++ b/dist/plugin.js @@ -0,0 +1 @@ +!function(t,e){function r(t){let e=t.substr(t.search(/[A-Z]/));return e[0].toLowerCase()+e.substr(1)}function n(t,e){let r=t.indexOf(e);-1!==r&&t.splice(r,1)}function c(e){return t[e].prototype||t[e]}t.plugin={register(t,n){const i=c(t);i._inc||(i._inc={},i._bInc=function(t,e,...r){return this._inc[e].before.reduce((e,r)=>{let n=r(t,...e);return n||e},r)},i._aInc=function(t,e,r,...n){return this._inc[e].after.reduce((e,r)=>{let c=r(t,e,...n);return c||e},r)}),e(n).forEach(t=>{let e=r(t);i[e]&&(i["_o"+e]||(i["_o"+e]=i[e],i[e]=function(...t){let r=this._bInc(this,e,...t),n=i["_o"+e].call(this,...r);return this._aInc(this,e,n,...t)}),i._inc[e]||(i._inc[e]={before:[],after:[]}),t.startsWith("before")?i._inc[e].before.push(n[t]):t.startsWith("after")&&i._inc[e].after.push(n[t]))})},unregister(t,i){const s=c(t);s._inc&&e(i).forEach(t=>{let e=r(t);t.startsWith("before")?n(s._inc[e].before,i[t]):t.startsWith("after")&&n(s._inc[e].after,i[t])})},extend(t,r){const n=c(t);e(r).forEach(t=>{n[t]||(n[t]=r[t])})}}}(kontra,Object.getOwnPropertyNames); \ No newline at end of file diff --git a/dist/pointer.js b/dist/pointer.js index 3d9c7e28..c0802d82 100644 --- a/dist/pointer.js +++ b/dist/pointer.js @@ -1 +1 @@ -!function(){let t,n=[],e=[],o={},a=[],i={},r={0:"left",1:"middle",2:"right"};function c(n){let e=n.x,o=n.y;n.anchor&&(e-=n.width*n.anchor.x,o-=n.height*n.anchor.y);let a=t.x-Math.max(e,Math.min(t.x,e+n.width)),i=t.y-Math.max(o,Math.min(t.y,o+n.height));return a*a+i*i=0;n--)if(a=(o=i[n]).collidesWithPointer?o.collidesWithPointer(t):c(o))return o}function s(t){let n=void 0!==t.button?r[t.button]:"left";i[n]=!0,f(t,"onDown")}function h(t){let n=void 0!==t.button?r[t.button]:"left";i[n]=!1,f(t,"onUp")}function d(t){f(t,"onOver")}function l(t){i={}}function f(n,e){if(!kontra.canvas)return;let a,i;-1!==["touchstart","touchmove","touchend"].indexOf(n.type)?(a=(n.touches[0]||n.changedTouches[0]).clientX,i=(n.touches[0]||n.changedTouches[0]).clientY):(a=n.clientX,i=n.clientY);let r,c=kontra.canvas.height/kontra.canvas.offsetHeight,s=kontra.canvas.getBoundingClientRect(),h=(a-s.left)*c,d=(i-s.top)*c;t.x=h,t.y=d,n.target===kontra.canvas&&(n.preventDefault(),(r=u())&&r[e]&&r[e](n)),o[e]&&o[e](n,r)}t=kontra.pointer={x:0,y:0,radius:5,track(t){[].concat(t).map(function(t){t._r||(t._r=t.render,t.render=function(){n.push(this),this._r()},a.push(t))})},untrack(t,n){[].concat(t).map(function(t){t.render=t._r,t._r=n;let e=a.indexOf(t);-1!==e&&a.splice(e,1)})},over:t=>-1!==a.indexOf(t)&&u()===t,onDown(t){o.onDown=t},onUp(t){o.onUp=t},pressed:t=>!!i[t]},kontra._tick=function(){e.length=0,n.map(function(t){e.push(t)}),n.length=0},kontra._init=function(){kontra.canvas.addEventListener("mousedown",s),kontra.canvas.addEventListener("touchstart",s),kontra.canvas.addEventListener("mouseup",h),kontra.canvas.addEventListener("touchend",h),kontra.canvas.addEventListener("blur",l),kontra.canvas.addEventListener("mousemove",d),kontra.canvas.addEventListener("touchmove",d)}}(); \ No newline at end of file +!function(){let t,n=[],e=[],o={},a=[],r={},i={0:"left",1:"middle",2:"right"};function c(n){let e=n.x,o=n.y;n.anchor&&(e-=n.width*n.anchor.x,o-=n.height*n.anchor.y);let a=t.x-Math.max(e,Math.min(t.x,e+n.width)),r=t.y-Math.max(o,Math.min(t.y,o+n.height));return a*a+r*r=0;n--)if(a=(o=r[n]).collidesWithPointer?o.collidesWithPointer(t):c(o))return o}function s(t){let n=void 0!==t.button?i[t.button]:"left";r[n]=!0,v(t,"onDown")}function h(t){let n=void 0!==t.button?i[t.button]:"left";r[n]=!1,v(t,"onUp")}function d(t){v(t,"onOver")}function l(t){r={}}function v(n,e){if(!kontra.canvas)return;let a,r;-1!==["touchstart","touchmove","touchend"].indexOf(n.type)?(a=(n.touches[0]||n.changedTouches[0]).clientX,r=(n.touches[0]||n.changedTouches[0]).clientY):(a=n.clientX,r=n.clientY);let i,c=kontra.canvas.height/kontra.canvas.offsetHeight,s=kontra.canvas.getBoundingClientRect(),h=(a-s.left)*c,d=(r-s.top)*c;t.x=h,t.y=d,n.target===kontra.canvas&&(n.preventDefault(),(i=u())&&i[e]&&i[e](n)),o[e]&&o[e](n,i)}t=kontra.pointer={x:0,y:0,radius:5,track(t){[].concat(t).map(function(t){t._r||(t._r=t.render,t.render=function(){n.push(this),this._r()},a.push(t))})},untrack(t,n){[].concat(t).map(function(t){t.render=t._r,t._r=n;let e=a.indexOf(t);-1!==e&&a.splice(e,1)})},over:t=>-1!==a.indexOf(t)&&u()===t,onDown(t){o.onDown=t},onUp(t){o.onUp=t},pressed:t=>!!r[t]},kontra.on("tick",()=>{e.length=0,n.map(function(t){e.push(t)}),n.length=0}),kontra.on("init",()=>{kontra.canvas.addEventListener("mousedown",s),kontra.canvas.addEventListener("touchstart",s),kontra.canvas.addEventListener("mouseup",h),kontra.canvas.addEventListener("touchend",h),kontra.canvas.addEventListener("blur",l),kontra.canvas.addEventListener("mousemove",d),kontra.canvas.addEventListener("touchmove",d)})}(); \ No newline at end of file diff --git a/dist/sprite.js b/dist/sprite.js index 433b8954..4db2d1cd 100644 --- a/dist/sprite.js +++ b/dist/sprite.js @@ -1 +1,5 @@ -!function(){class t{constructor(t,i){this._x=t||0,this._y=i||0}add(t,i){this.x+=(t.x||0)*(i||1),this.y+=(t.y||0)*(i||1)}clamp(t,i,h,s){this._c=!0,this._a=t,this._b=i,this._d=h,this._e=s}get x(){return this._x}get y(){return this._y}set x(t){this._x=this._c?Math.min(Math.max(this._a,t),this._d):t}set y(t){this._y=this._c?Math.min(Math.max(this._b,t),this._e):t}}kontra.vector=((i,h)=>new t(i,h)),kontra.vector.prototype=t.prototype;class i{init(t,i,h,s){for(i in t=t||{},this.position=kontra.vector(t.x,t.y),this.velocity=kontra.vector(t.dx,t.dy),this.acceleration=kontra.vector(t.ddx,t.ddy),this.width=this.height=this.rotation=0,this.ttl=1/0,this.anchor={x:0,y:0},this.context=kontra.context,t)this[i]=t[i];return(h=t.image)&&(this.image=h,this.width=void 0!==t.width?t.width:h.width,this.height=void 0!==t.height?t.height:h.height),this}get x(){return this.position.x}get y(){return this.position.y}get dx(){return this.velocity.x}get dy(){return this.velocity.y}get ddx(){return this.acceleration.x}get ddy(){return this.acceleration.y}get animations(){return this.anims}set x(t){this.position.x=t}set y(t){this.position.y=t}set dx(t){this.velocity.x=t}set dy(t){this.velocity.y=t}set ddx(t){this.acceleration.x=t}set ddy(t){this.acceleration.y=t}set animations(t){let i,h;for(i in this.anims={},t)this.anims[i]=t[i].clone(),h=h||this.anims[i];this.currentAnimation=h,this.width=this.width||h.width,this.height=this.height||h.height}isAlive(){return this.ttl>0}collidesWith(t){if(this.rotation||t.rotation)return null;let i=this.x-this.width*this.anchor.x,h=this.y-this.height*this.anchor.y,s=t.x,e=t.y;return t.anchor&&(s-=t.width*t.anchor.x,e-=t.height*t.anchor.y),is&&he}update(t){this.advance(t)}render(){this.draw()}playAnimation(t){this.currentAnimation=this.animations[t],this.currentAnimation.loop||this.currentAnimation.reset()}advance(t){this.velocity.add(this.acceleration,t),this.position.add(this.velocity,t),this.ttl--,this.currentAnimation&&this.currentAnimation.update(t)}draw(){let t=-this.width*this.anchor.x,i=-this.height*this.anchor.y;this.context.save(),this.context.translate(this.x,this.y),this.rotation&&this.context.rotate(this.rotation),this.image?this.context.drawImage(this.image,0,0,this.image.width,this.image.height,t,i,this.width,this.height):this.currentAnimation?this.currentAnimation.render({x:t,y:i,width:this.width,height:this.height,context:this.context}):(this.context.fillStyle=this.color,this.context.fillRect(t,i,this.width,this.height)),this.context.restore()}}kontra.sprite=(t=>(new i).init(t)),kontra.sprite.prototype=i.prototype}(); \ No newline at end of file +<<<<<<< HEAD +!function(){class t{constructor(t,i){this._x=t||0,this._y=i||0}add(t,i){return kontra.vector(this.x+(t.x||0)*(i||1),this.y+(t.y||0)*(i||1))}clamp(t,i,h,s){this._c=!0,this._a=t,this._b=i,this._d=h,this._e=s}get x(){return this._x}get y(){return this._y}set x(t){this._x=this._c?Math.min(Math.max(this._a,t),this._d):t}set y(t){this._y=this._c?Math.min(Math.max(this._b,t),this._e):t}}kontra.vector=((i,h)=>new t(i,h)),kontra.vector.prototype=t.prototype;class i{init(t,i,h,s){for(i in t=t||{},this.position=kontra.vector(t.x,t.y),this.velocity=kontra.vector(t.dx,t.dy),this.acceleration=kontra.vector(t.ddx,t.ddy),this.width=this.height=this.rotation=0,this.ttl=1/0,this.anchor={x:0,y:0},this.context=kontra.context,t)this[i]=t[i];if(h=t.image)this.image=h,this.width=void 0!==t.width?t.width:h.width,this.height=void 0!==t.height?t.height:h.height;else if(h=t.animations){for(i in h)this.animations[i]=h[i].clone(),s=s||h[i];this._ca=s,this.width=this.width||s.width,this.height=this.height||s.height}return this}get x(){return this.position.x}get y(){return this.position.y}get dx(){return this.velocity.x}get dy(){return this.velocity.y}get ddx(){return this.acceleration.x}get ddy(){return this.acceleration.y}set x(t){this.position.x=t}set y(t){this.position.y=t}set dx(t){this.velocity.x=t}set dy(t){this.velocity.y=t}set ddx(t){this.acceleration.x=t}set ddy(t){this.acceleration.y=t}isAlive(){return this.ttl>0}collidesWith(t){if(this.rotation||t.rotation)return null;let i=this.x-this.width*this.anchor.x,h=this.y-this.height*this.anchor.y,s=t.x,e=t.y;return t.anchor&&(s-=t.width*t.anchor.x,e-=t.height*t.anchor.y),is&&he}update(t){this.advance(t)}render(){this.draw()}playAnimation(t){this._ca=this.animations[t],this._ca.loop||this._ca.reset()}advance(t){this.velocity=this.velocity.add(this.acceleration,t),this.position=this.position.add(this.velocity,t),this.ttl--,this._ca&&this._ca.update(t)}draw(){let t=-this.width*this.anchor.x,i=-this.height*this.anchor.y;this.context.save(),this.context.translate(this.x,this.y),this.rotation&&this.context.rotate(this.rotation),this.image?this.context.drawImage(this.image,0,0,this.image.width,this.image.height,t,i,this.width,this.height):this._ca?this._ca.render({x:t,y:i,width:this.width,height:this.height,context:this.context}):(this.context.fillStyle=this.color,this.context.fillRect(t,i,this.width,this.height)),this.context.restore()}}kontra.sprite=(t=>(new i).init(t)),kontra.sprite.prototype=i.prototype}(); +======= +!function(){class t{constructor(t,i){this._x=t||0,this._y=i||0}add(t,i){this.x+=(t.x||0)*(i||1),this.y+=(t.y||0)*(i||1)}clamp(t,i,h,s){this._c=!0,this._a=t,this._b=i,this._d=h,this._e=s}get x(){return this._x}get y(){return this._y}set x(t){this._x=this._c?Math.min(Math.max(this._a,t),this._d):t}set y(t){this._y=this._c?Math.min(Math.max(this._b,t),this._e):t}}kontra.vector=((i,h)=>new t(i,h)),kontra.vector.prototype=t.prototype;class i{init(t,i,h,s){for(i in t=t||{},this.position=kontra.vector(t.x,t.y),this.velocity=kontra.vector(t.dx,t.dy),this.acceleration=kontra.vector(t.ddx,t.ddy),this.width=this.height=this.rotation=0,this.ttl=1/0,this.anchor={x:0,y:0},this.context=kontra.context,t)this[i]=t[i];return(h=t.image)&&(this.image=h,this.width=void 0!==t.width?t.width:h.width,this.height=void 0!==t.height?t.height:h.height),this}get x(){return this.position.x}get y(){return this.position.y}get dx(){return this.velocity.x}get dy(){return this.velocity.y}get ddx(){return this.acceleration.x}get ddy(){return this.acceleration.y}get animations(){return this.anims}set x(t){this.position.x=t}set y(t){this.position.y=t}set dx(t){this.velocity.x=t}set dy(t){this.velocity.y=t}set ddx(t){this.acceleration.x=t}set ddy(t){this.acceleration.y=t}set animations(t){let i,h;for(i in this.anims={},t)this.anims[i]=t[i].clone(),h=h||this.anims[i];this.currentAnimation=h,this.width=this.width||h.width,this.height=this.height||h.height}isAlive(){return this.ttl>0}collidesWith(t){if(this.rotation||t.rotation)return null;let i=this.x-this.width*this.anchor.x,h=this.y-this.height*this.anchor.y,s=t.x,e=t.y;return t.anchor&&(s-=t.width*t.anchor.x,e-=t.height*t.anchor.y),is&&he}update(t){this.advance(t)}render(){this.draw()}playAnimation(t){this.currentAnimation=this.animations[t],this.currentAnimation.loop||this.currentAnimation.reset()}advance(t){this.velocity.add(this.acceleration,t),this.position.add(this.velocity,t),this.ttl--,this.currentAnimation&&this.currentAnimation.update(t)}draw(){let t=-this.width*this.anchor.x,i=-this.height*this.anchor.y;this.context.save(),this.context.translate(this.x,this.y),this.rotation&&this.context.rotate(this.rotation),this.image?this.context.drawImage(this.image,0,0,this.image.width,this.image.height,t,i,this.width,this.height):this.currentAnimation?this.currentAnimation.render({x:t,y:i,width:this.width,height:this.height,context:this.context}):(this.context.fillStyle=this.color,this.context.fillRect(t,i,this.width,this.height)),this.context.restore()}}kontra.sprite=(t=>(new i).init(t)),kontra.sprite.prototype=i.prototype}(); +>>>>>>> v6 diff --git a/docs/api/gameLoop.html b/docs/api/gameLoop.html index 00db06c3..0d1dffa7 100644 --- a/docs/api/gameLoop.html +++ b/docs/api/gameLoop.html @@ -69,6 +69,12 @@

Kontra​.gameLoop(​properties)

Table of Contents

    +
  • + +
  • +
  • Properties

      @@ -92,6 +98,20 @@

      Methods

      +
      +

      Lifecycle Events

      + +

      Every frame it will emit a tick event.

      + +
      kontra.on('tick', () => {
      +  // run code every frame
      +});
      +
      + + + + +

      kontra.gameLoop​.isStopped

      diff --git a/docs/api/kontra.html b/docs/api/kontra.html index 6a7c0f2a..e9d9269c 100644 --- a/docs/api/kontra.html +++ b/docs/api/kontra.html @@ -36,6 +36,12 @@

      Kontra

      Table of Contents

        +
      • + +
      • +
      • Properties

        @@ -51,7 +58,10 @@

        Properties

      • Methods

          +
        • kontra​.emit(event[, ...args])
        • kontra​.init([canvas])
        • +
        • kontra​.off(event, callback)
        • +
        • kontra​.on(event, callback)
        • kontra​.animation(properties)
        • kontra​.gameLoop(properties)
        • kontra​.pool(properties)
        • @@ -69,6 +79,20 @@

          Methods

          +
          +

          Lifecycle Events

          + +

          When Kontra is initialized, it will emit an init event.

          + +
          kontra.on('init', (canvas) => {
          +  // run code when Kontra is initialized
          +});
          +
          + + + + +

          kontra.canvas

          @@ -91,6 +115,26 @@

          kontra.context +

          kontra​.emit(event[, ...args])

          + +
          +
          event {string}
          +
          The name of the event to emit.
          +
          args {...*}
          +
          Arguments passed to all callbacks.
          +
          + +

          Emit an event and run all its callbacks, passing it any arguments.

          + +
          kontra.emit('myEvent', 1, 2, 'three');
          + +

          + + + + +

          kontra.init([canvas])

          @@ -103,6 +147,47 @@

          kontra.init([canvas]) + + + + +
          +

          kontra​.off(event, callback)

          + +
          +
          event {string}
          +
          The name of the event to emit.
          +
          callback {function}
          +
          Function callback.
          +
          + +

          Remove the callback from the event so it will no longer be run when the event is emitted.

          + +
          + + + + + +
          +

          kontra​.on(event, callback)

          + +
          +
          event {string}
          +
          The name of the event to emit.
          +
          callback {function}
          +
          Function callback.
          +
          + +

          Add the callback to the event so it will run when the event is emitted. The callback function will be passed all arguments passed to the emit function.

          + +
          kontra.on('myEvent', (param1, param2, param3) => {
          +  // ...
          + });
          +
          +kontra.emit('myEvent', 1, 2, 'three');
          + +
          diff --git a/docs/api/plugin.html b/docs/api/plugin.html new file mode 100644 index 00000000..e106a850 --- /dev/null +++ b/docs/api/plugin.html @@ -0,0 +1,238 @@ + + + + Kontra.js – Kontra.plugin + + + + + + + + + + + + + + + +
          + + +
          +
          +

          Kontra.plugin

          + +

          A minimal plugin architecture to enable sharing of common functionality. The plugin system is designed after the Interceptor Pattern and allows you to write functions that can be run before and after any Kontra object function.

          + +

          The plugin architecture also allows you to safely extend Kontra objects. This allows you to safely add additional properties without overriding ones that may already exist.

          + +
          let myPlugin = {
          +  beforeAdd(vector, vecToAdd) {
          +    console.log(`adding {${vecToAdd.x}, ${vecToAdd.y}} vector to {${vector.x}, ${vector.y}}`);
          +  },
          +  afterAdd(vector, result, vecToAdd) {
          +    console.log(`result of add was: {${result.x}, ${result.y}}`);
          +  }
          +};
          +
          +kontra.plugin.register('vector', myPlugin);
          + + + + + +
          +

          Table of Contents

          + + +
          + + + + + +
          +

          Plugin Best Practices

          + +
            +
          • Export your plugin object for the consumer to use.
          • +
          • You should not register the plugin yourself. Instead, you should expect the consumer of your plugin to register it. This way the consumer can determine the order in which plugins should run.
          • +
          • In your plugin docs, remind the user to register the plugin themselves.
          • +
          • Gracefully handle errors in in your plugin code. As an idea, you could return null on an error so your plugin does not break the rest of the plugin chain.
          • +
          + +
          (function() {
          +  let myPlugin = {
          +    beforeAdd() {
          +      // ...
          +    },
          +    afterAdd() {
          +      // ...
          +    }
          +  }
          +
          +  window.myPlugin = myPlugin;
          +  // or
          +  module.export = myPlugin;
          +  // or
          +  export default myPlugin;
          +})();
          + +
          + + + + + +
          +

          kontra.plugin​.extend(object, properties)

          + +
          +
          object {string}
          +
          Kontra object to add extend.
          +
          properties {object}
          +
          Properties to add to the Kontra object.
          +
          + +

          Safely extend a Kontra object with the provided properties. If the property already exists on the object, your property will not be added.

          + +
          kontra.plugin.extend('vector', {
          +  subtract: function(vec) {
          +    return kontra.vector(this.x - vec.x, this.y - vec.y);
          +  }
          +};  // all kontra.vectors will now have a subtract function
          + +
          + + + + + +
          +

          kontra.plugin​.register(object, plugin)

          + +
          +
          object {string}
          +
          Kontra object to attach plugin to.
          +
          plugin {object}
          +
          Plugin object with before and after functions.
          +
          + +

          Register a plugin to run before and/or after any function defined on the Kontra object.

          + +
          // Intercept kontra.vector.add function and run functions before and after it
          +let myPlugin = {
          +  beforeAdd(vector, vecToAdd) {
          +    console.log(`adding {${vecToAdd.x}, ${vecToAdd.y}} vector to {${vector.x}, ${vector.y}}`);
          +  },
          +  afterAdd(vector, result, vecToAdd) {
          +    console.log(`result of add was: {${result.x}, ${result.y}}`);
          +  }
          +};
          +
          +kontra.plugin.register('vector', myPlugin);
          + +

          Before functions

          + +

          Before functions run before the intercepted Kontra object function. They are defined by prefixing the function with before and the name of the intercepted function. The parameters passed to the function will be the context the function was called with followed by all parameters passed to the intercepted function.

          + +

          Before functions can modify the arguments passed to the intercepted function. To do this, you'll need to return an array that represents the new arguments. You can modify or pass through any argument in this way. If your function does not modify the arguments, you can return null.

          + +

          If multiple before functions are registered for the same intercepted function, they will be run in the order they were registered and will be passed the arguments from the previous before function.

          + +
          let logPlugin = {
          +  // `vector` is the vector the `add` function was called on
          +  // `vecToAdd` is the vector being passed to the `add` function
          +  beforeAdd(vector, vecToAdd) {
          +    console.log(`adding {${vecToAdd.x}, ${vecToAdd.y}} vector to {${vector.x}, ${vector.y}}`);
          +
          +    // don't modify the arguments so return nothing
          +  }
          +}
          +
          +let modifyArgsPlugin = {
          +  beforeAdd(vector, vecToAdd) {
          +    // modify the parameter being passed to the `add` function
          +    let newVector = kontra.vector(10, 20);
          +    return [newVector]
          +  }
          +}
          +
          +let passThroughArgsPlugin = {
          +  beforeAdd(vector, vecToAdd) {
          +    // pass through the original argument to the `add` function but also
          +    // pass an additional one
          +    let newVector = kontra.vector(10, 20);
          +    return [vecToAdd, newVector]
          +  }
          +}
          + +

          After functions

          + +

          After functions run after the intercepted Kontra object function. They are defined by prefixing the function with after and the name of the intercepted function. The parameters passed to the function will be the context the function was called with, the result of the intercepted function, followed by all parameters passed to the intercepted function.

          + +

          After functions can modify the result of the intercepted function. To do this, you return a new value as the result. If your function does not modify the result, you can return null.

          + +

          If multiple after functions are registered for the same intercepted function, they will be run in the order they were registered and will be passed the result from the previous after function.

          + +
          let logPlugin = {
          +  // `vector` is the vector the `add` function was called on
          +  // `result` is the result of the `add` function
          +  // `vecToAdd` is the vector being passed to the `add` function
          +  afterAdd(vector, result, vecToAdd) {
          +    console.log(`result of add was: {${result.x}, ${result.y}}`);
          +
          +    // don't modify the result so return nothing
          +  }
          +}
          +
          +let modifyResultPlugin = {
          +  afterAdd(vector, result, vecToAdd) {
          +    // modify the result of the `add` function
          +    return kontra.vector(result.x * 2, result.y * 2);
          +  }
          +}
          + +
          + + + + + +
          +

          kontra.plugin​.unregsiter(object, plugin)

          + +
          +
          object {string}
          +
          Kontra object to attach plugin to.
          +
          plugin {object}
          +
          Plugin object with before and after functions.
          +
          + +

          Unregister a plugin and remove all its before and after functions from the intercepted Kontra object.

          + +
          + +
          +
          +
          + + + \ No newline at end of file diff --git a/docs/components/main-nav.html b/docs/components/main-nav.html index cc6b3eb4..d6946a58 100644 --- a/docs/components/main-nav.html +++ b/docs/components/main-nav.html @@ -23,6 +23,7 @@

          API

        • Keyboard
        • Kontra
        • Object Pool
        • +
        • Plugin
        • Pointer
        • Quadtree
        • Sprite
        • diff --git a/docs/download.html b/docs/download.html index 88cdd91e..e64749a5 100644 --- a/docs/download.html +++ b/docs/download.html @@ -80,6 +80,11 @@

          Download

          Object pool + +