Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Beyond Boxes - Creative coding with JavaScript

Beyond Boxes - Creative coding with JavaScript

Rachel Smith

October 30, 2015
Tweet

More Decks by Rachel Smith

Other Decks in Technology

Transcript

  1. “Designers are less willing to take risks… Andrew Clarke “Creativity

    over predictability” - 2014 …Our work for the web has lost its creative soul”
  2. I’m constantly on the look out for fresh examples of:

    Media integration animation graphics (Canvas & WebGL)
  3. I fell in love with “demo culture”. Code as not

    just a means to an end. Code as art.
  4. // select canvas
 var canvas = document.getElementById('canvas');
 var context =

    canvas.getContext('2d');
 
 canvas.width = window.innerWidth;
 canvas.height = window.innerHeight;
 
 // draw a rectangle
 context.rect(10, 10, 100, 100);
 context.stroke();
  5. 
 
 function loop() { // things to happen in

    the frame go here requestAnimationFrame(loop);
 }
 
 loop();
  6. 
 
 
 function drawSquare(x, y) {
 context.beginPath();
 context.rect(x, y,

    100, 100);
 context.stroke();
 context.closePath();
 }
  7. // position
 var x = 10;
 var y = 10;


    // velocity
 var vx = 1;
 var vy = 1;
  8. function loop() {
 // clear the canvas
 context.clearRect(0, 0, width,

    height);
 // update the position using velocity
 x = x + vx;
 y = y + vy;
 // draw the square
 drawSquare(x, y);
 requestAnimationFrame(loop);
 }
  9. Square.prototype.update = function() {
 // update position
 this.position.x += this.velocity.x;


    this.position.y += this.velocity.y;
 // update size
 this.size += 0.05;
 }
  10. Square.prototype.draw = function() {
 // draw the square
 context.beginPath();
 context.rect(this.position.x,

    this.position.y, this.size, this.size);
 context.strokeStyle = this.color;
 context.stroke();
 context.closePath();
 }
  11. // create the squares
 for (var i = 0; i

    < 200; i++) {
 var square = new Square({
 x: -100,
 y: -100
 }, {
 x: 0,
 y: 0
 }, 30, 'white');
 squares.push(square);
 }
  12. // create the squares
 for (var i = 0; i

    < 200; i++) {
 var square = new Square({
 x: -100,
 y: -100
 }, {
 x: 0,
 y: 0
 }, 30, 'white');
 squares.push(square); startSquare(square, i * 200); }
  13. function startSquare(square, delay) {
 setTimeout(function() {
 square.size = 30;
 square.velocity.x

    = 0.5 + Math.random() * 1;
 square.velocity.y = 0.5 + Math.random() * 1;
 }, delay);
 }
  14. function loop() {
 context.clearRect(0, 0, width, height);
 var l =

    squares.length;
 for (var i = 0; i < l; i++) {
 squares[i].update();
 squares[i].draw();
 } 
 requestAnimationFrame(loop);
 }
  15. for (var i = 0; i < 200; i++) {


    var hue = Math.round(360 / 30 * i);
 var color = 'hsl(' + hue + ', 50%, 50%)';
 var square = new Square({
 x: -100,
 y: -100
 }, {
 x: 0,
 y: 0
 }, 30, color);
 squares.push(square);
 startSquare(square, i * 200);
 }
  16. function Square(position, velocity, size, color, vRotation) {
 this.position = position;


    this.velocity = velocity;
 this.size = size;
 this.color = color; this.rotation = 0; this.vRotation = vRotation;
 }
  17. Square.prototype.update = function() {
 // update position
 this.position.x += this.velocity.x;


    this.position.y += this.velocity.y;
 // update size
 this.size += 0.05;
 // update rotation
 this.rotation += this.vRotation;
 }
  18. Square.prototype.draw = function() {
 // save canvas coordinate system
 context.save();


    // translate to center of square
 context.translate(this.position.x + this.size * 0.5, this.position.y + this.size * 0.5);
 // rotate the canvas
 context.rotate(this.rotation * Math.PI / 180);
 // translate back
 context.translate(-(this.position.x + this.size * 0.5), - (this.position.y + this.size * 0.5));
 
 // draw the square
 ...
 
 // restore the coordinate system
 context.restore();
 }
  19. DIY with the Canvas API can get a little tedious.

    Fabric.js Easel.js Paper.js many more…
  20. var canvas = new fabric.Canvas('c');
 
 // create a rectangle

    with angle=45
 var rect = new fabric.Rect({
 left: 10,
 top: 10,
 stroke: 'white',
 width: 100,
 height: 100,
 angle: 45
 });
 
 canvas.add(rect);
  21. “What do you mean I can’t jam 2000 SVG elements

    in to a document with a blur filter??”
  22. The simple question of “why are my fans on?” requestAnimationFrame

    JavaScript memory management layout, composition, paint in the DOM
  23. It’s not even just a browser on your phone! (Or

    on your fridge, or on your Apple Watch).