topical media & game development

talk show tell print

mobile-game-ch21-scribble-public-scribble.js / js



  $(function() {
  
    var Q = Quintus().setup('quintus', { maximize: true }),
        socket = io.connect(),
        start = {}, 
        move = {};
  
    function getTouch(e) {
      var touch = e.originalEvent.changedTouches ? 
                  e.originalEvent.changedTouches[0] : e,
          canvasPos = Q.el.offset(),
          canvasX = (touch.pageX - canvasPos.left) / Q.el.width() * Q.width,
          canvasY = (touch.pageY - canvasPos.top) / Q.el.height() * Q.height;
  
      e.preventDefault();
      return { x: canvasX, y: canvasY };
    }
  
    function drawLine(from,to) {
      Q.ctx.strokeStyle= "#000";
      Q.ctx.beginPath();
      Q.ctx.moveTo(from.x,from.y);
      Q.ctx.lineTo(to.x,to.y);
      Q.ctx.stroke();
    }
  
    Q.el.on('touchstart mousedown',function(e) {
      start = getTouch(e);
    });
  
    Q.el.on('touchmove mousemove',function(e) {
      if(!start.x) return;
      move = getTouch(e);
  
      drawLine(start,move);
  
      socket.emit("paint",{ start: start, move: move });
      start = move;
    });
  
    Q.el.on('touchend mouseup mouseleave',function(e) {
      start.x = null;
    });
  
    socket.on("connect",function() {
      console.log("Connected");
    });
  
    socket.on("paint",function(data) {
      drawLine(data.start,data.move);
    });
  
    socket.on("clear",function(data) {
      Q.ctx.clearRect(0,0,Q.width,Q.height);
    });
    
  
  });
  


(C) Æliens 04/09/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.