File size: 8,055 Bytes
7f88cfb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/**
 * @module Rendering
 * @submodule Rendering
 * @for p5
 */
define(function(require) {

  var p5 = require('core/core');
  var constants = require('core/constants');
  require('core/p5.Graphics');
  require('core/p5.Renderer2D');
  require('3d/p5.Renderer3D');

  /**
   * Creates a canvas element in the document, and sets the dimensions of it
   * in pixels. This method should be called only once at the start of setup.
   * Calling createCanvas more than once in a sketch will result in very
   * unpredicable behavior. If you want more than one drawing canvas
   * you could use createGraphics (hidden by default but it can be shown).<br>
   * The system variables width and height are set by the parameters passed
   * to this function. If createCanvas() is not used, the window will be
   * given a default size of 100x100 pixels.
   *
   * @method createCanvas
   * @param  {Number} w width of the canvas
   * @param  {Number} h height of the canvas
   * @param  optional:{String} renderer 'p2d' | 'webgl'
   * @return {Object} canvas generated
   * @example
   * <div>
   * <code>
   * function setup() {
   *   createCanvas(100, 50);
   *   background(153);
   *   line(0, 0, width, height);
   * }
   * </code>
   * </div>
   */

  p5.prototype.createCanvas = function(w, h, renderer) {
    //optional: renderer, otherwise defaults to p2d
    var r = renderer || constants.P2D;
    var isDefault, c;

    //4th arg (isDefault) used when called onLoad,
    //otherwise hidden to the public api
    if(arguments[3]){
      isDefault =
      (typeof arguments[3] === 'boolean') ? arguments[3] : false;
    }

    if(r === constants.WEBGL){
      c = document.getElementById('defaultCanvas');
      if(c){ //if defaultCanvas already exists
        c.parentNode.removeChild(c); //replace the existing defaultCanvas
      }
      c = document.createElement('canvas');
      c.id = 'defaultCanvas';
    }
    else {
      if (isDefault) {
        c = document.createElement('canvas');
        c.id = 'defaultCanvas';
      } else { // resize the default canvas if new one is created
        c = this.canvas;
      }
    }

    // set to invisible if still in setup (to prevent flashing with manipulate)
    if (!this._setupDone) {
      c.className += ' p5_hidden'; // tag to show later
      c.style.visibility='hidden';
    }

    if (this._userNode) { // user input node case
      this._userNode.appendChild(c);
    } else {
      document.body.appendChild(c);
    }
    


    // Init our graphics renderer
    //webgl mode
    if (r === constants.WEBGL) {
      this._setProperty('_graphics', new p5.Renderer3D(c, this, true));
      this._isdefaultGraphics = true;
    }
    //P2D mode
    else {
      if (!this._isdefaultGraphics) {
        this._setProperty('_graphics', new p5.Renderer2D(c, this, true));
        this._isdefaultGraphics = true;
      }
    }
    this._graphics.resize(w, h);
    this._graphics._applyDefaults();
    return this._graphics;
  };

  /**
   * Resizes the canvas to given width and height. Note that the
   * canvas will be cleared so anything drawn previously in setup
   * or draw will disappear on resize. Setup will not be called
   * again.
   * @method resizeCanvas
   * @example
   * <div class="norender"><code>
   * function setup() {
   *   createCanvas(windowWidth, windowHeight);
   * }
   *
   * function draw() {
   *  background(0, 100, 200);
   * }
   *
   * function windowResized() {
   *   resizeCanvas(windowWidth, windowHeight);
   * }
   * </code></div>
   */
  p5.prototype.resizeCanvas = function (w, h, noRedraw) {
    if (this._graphics) {
      this._graphics.resize(w, h);
      this._graphics._applyDefaults();
      if (!noRedraw) {
        this.redraw();
      }
    }
  };


  /**
   * Removes the default canvas for a p5 sketch that doesn't
   * require a canvas
   * @method noCanvas
   * @example
   * <div>
   * <code>
   * function setup() {
   *   noCanvas();
   * }
   * </code>
   * </div>
   */
  p5.prototype.noCanvas = function() {
    if (this.canvas) {
      this.canvas.parentNode.removeChild(this.canvas);
    }
  };

  /**
   * Creates and returns a new p5.Renderer object. Use this class if you need
   * to draw into an off-screen graphics buffer. The two parameters define the
   * width and height in pixels.
   *
   * @method createGraphics
   * @param  {Number} w width of the offscreen graphics buffer
   * @param  {Number} h height of the offscreen graphics buffer
   * @param {String} renderer either 'p2d' or 'webgl'.
   * undefined defaults to p2d
   * @return {Object} offscreen graphics buffer
   * @example
   * <div>
   * <code>
   * var pg;
   * function setup() {
   *   createCanvas(100, 100);
   *   pg = createGraphics(100, 100);
   * }
   * function draw() {
   *   background(200);
   *   pg.background(100);
   *   pg.noStroke();
   *   pg.ellipse(pg.width/2, pg.height/2, 50, 50);
   *   image(pg, 50, 50);
   *   image(pg, 0, 0, 50, 50);
   * }
   * </code>
   * </div>
   */
  p5.prototype.createGraphics = function(w, h, renderer){
    return new p5.Graphics(w, h, renderer, this);
  };

  /**
   * Blends the pixels in the display window according to the defined mode.
   * There is a choice of the following modes to blend the source pixels (A)
   * with the ones of pixels already in the display window (B):
   * <ul>
   * <li><code>BLEND</code> - linear interpolation of colours: C =
   * A*factor + B. This is the default blending mode.</li>
   * <li><code>ADD</code> - sum of A and B</li>
   * <li><code>DARKEST</code> - only the darkest colour succeeds: C =
   * min(A*factor, B).</li>
   * <li><code>LIGHTEST</code> - only the lightest colour succeeds: C =
   * max(A*factor, B).</li>
   * <li><code>DIFFERENCE</code> - subtract colors from underlying image.</li>
   * <li><code>EXCLUSION</code> - similar to <code>DIFFERENCE</code>, but less
   * extreme.</li>
   * <li><code>MULTIPLY</code> - multiply the colors, result will always be
   * darker.</li>
   * <li><code>SCREEN</code> - opposite multiply, uses inverse values of the
   * colors.</li>
   * <li><code>REPLACE</code> - the pixels entirely replace the others and
   * don't utilize alpha (transparency) values.</li>
   * <li><code>OVERLAY</code> - mix of <code>MULTIPLY</code> and <code>SCREEN
   * </code>. Multiplies dark values, and screens light values.</li>
   * <li><code>HARD_LIGHT</code> - <code>SCREEN</code> when greater than 50%
   * gray, <code>MULTIPLY</code> when lower.</li>
   * <li><code>SOFT_LIGHT</code> - mix of <code>DARKEST</code> and
   * <code>LIGHTEST</code>. Works like <code>OVERLAY</code>, but not as harsh.
   * </li>
   * <li><code>DODGE</code> - lightens light tones and increases contrast,
   * ignores darks.</li>
   * <li><code>BURN</code> - darker areas are applied, increasing contrast,
   * ignores lights.</li>
   * </ul>
   *
   * @method blendMode
   * @param  {String/Constant} mode blend mode to set for canvas
   * @example
   * <div>
   * <code>
   * blendMode(LIGHTEST);
   * strokeWeight(30);
   * stroke(80, 150, 255);
   * line(25, 25, 75, 75);
   * stroke(255, 50, 50);
   * line(75, 25, 25, 75);
   * </code>
   * </div>
   * <div>
   * <code>
   * blendMode(MULTIPLY);
   * strokeWeight(30);
   * stroke(80, 150, 255);
   * line(25, 25, 75, 75);
   * stroke(255, 50, 50);
   * line(75, 25, 25, 75);
   * </code>
   * </div>
   */
  p5.prototype.blendMode = function(mode) {
    if (mode === constants.BLEND || mode === constants.DARKEST ||
      mode === constants.LIGHTEST || mode === constants.DIFFERENCE ||
      mode === constants.MULTIPLY || mode === constants.EXCLUSION ||
      mode === constants.SCREEN || mode === constants.REPLACE ||
      mode === constants.OVERLAY || mode === constants.HARD_LIGHT ||
      mode === constants.SOFT_LIGHT || mode === constants.DODGE ||
      mode === constants.BURN || mode === constants.ADD ||
      mode === constants.NORMAL) {
      this._graphics.blendMode(mode);
    } else {
      throw new Error('Mode '+mode+' not recognized.');
    }
  };

  return p5;

});