Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*global window Processing*/
  2. (function (module) {
  3.     /**
  4.      * Used to build the attach options.
  5.      * @param object $ Contains a reference to thorny
  6.      * @param object options Contains the attachment specific options
  7.      * @return object Containing the attached options
  8.      */
  9.     var attachOptions = function ($, options) {
  10.         if (typeof options !== 'object') {
  11.             options = {};
  12.         }
  13.         return $._.extend((function () {
  14.             return {
  15.                 element: false,
  16.                 width: 320,
  17.                 height: 240
  18.             };
  19.         }()), options);
  20.     };
  21.    
  22.     module.exports = function ($) {
  23.         return {
  24.             attach: function (options) {
  25.                 options = attachOptions($, options);
  26.                
  27.                 var
  28.                     canvas = window.document.getElementById(options.element),
  29.                     pjs = new Processing(canvas);
  30.                    
  31.                 /**
  32.                  * Setup Processing.js
  33.                  * @param void
  34.                  * @return void
  35.                  */
  36.                 pjs.setup = function () {
  37.                     pjs.size(options.width, options.height);
  38.                     pjs.noLoop();
  39.                 };
  40.                
  41.                 /**
  42.                  * Setup the draw function for Processing.js
  43.                  * @param void
  44.                  * @return void
  45.                  */
  46.                 pjs.draw = function () {
  47.                     // partially clear, by overlaying a semi-transparent rect  
  48.                     // with background color  
  49.                     pjs.noStroke();  
  50.                     pjs.fill(102, 51, 0);  
  51.                     pjs.rect(0, 0, options.width, options.height);  
  52.                     // draw the "sine wave"
  53.                     pjs.stroke(51, 153, 0);
  54.                     pjs.fill(51, 153, 0);
  55.                    
  56.                     // Render the world
  57.                     $.getTag('world')
  58.                         .getComponent('load-level')
  59.                         .each(function (level) {
  60.                             var
  61.                                 polys = level.data.iterator(),
  62.                                 poly,
  63.                                 vectors;
  64.                            
  65.                             while ((poly = polys.step())) {
  66.                                 vectors = poly.node.getVector2s();
  67.                                
  68.                                 pjs.triangle(
  69.                                     vectors[0].getX(), vectors[0].getY(),
  70.                                     vectors[1].getX(), vectors[1].getY(),
  71.                                     vectors[2].getX(), vectors[2].getY()
  72.                                     );
  73.                             }
  74.                         });
  75.                    
  76.                     // Draw the drawable items to the screen
  77.                     $.es().searchByComponents('position', 'drawable')
  78.                         .each(function (position, drawable) {
  79.                             pjs.ellipse(
  80.                                 entity.data.getX(),
  81.                                 entity.data.getY(),
  82.                                 16,
  83.                                 16
  84.                                 );
  85.                         });
  86.                 };
  87.                
  88.                 pjs.setup();
  89.                
  90.                 return function () {
  91.                     pjs.redraw();
  92.                 };
  93.             }
  94.         };
  95.     };
  96. }((typeof window === 'undefined') ? module : window.thorny_path('./thorny/browser/renderer/processing')));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement