SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Apr 2013
    Location
    KL
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy Can't emulate touch events

    Hi. I'm trying to emulate touch events in Chrome for daaaaays but nothing happens. Can anybody tell me what's wrong? Thanks.

    Code:
    var cvs = null;
    var cb_ctx = null;
    var cb_lastPoints = null;
    var cb_easing = 0.4;
    
    // Setup event handlers
    window.onload = init;
    function init(e) {
    	cb_canvas = document.getElementById("cbook");
    
    	cb_lastPoints = Array();
    
    	if (cb_canvas.getContext) {
    		cb_ctx = cvs.getContext('2d');
    		cb_ctx.lineWidth = 2;
    		cb_ctx.strokeStyle = "rgb(0, 0, 0)";
    		cb_ctx.beginPath();
    
    		cvs.onmousedown = startDraw;
    		cvs.onmouseup = stopDraw;
    		cvs.ontouchstart = startDraw;
    		cvs.ontouchstop = stopDraw;
    		cvs.ontouchmove = drawMouse;
    	}
    }
    
    function startDraw(e) {
    	if (e.touches) {
    		// Touch event
    		for (var i = 1; i <= e.touches.length; i++) {
    			cb_lastPoints[i] = getCoords(e.touches[i - 1]); // Get info for finger #1
    		}
    	}
    	else {
    		// Mouse event
    		cb_lastPoints[0] = getCoords(e);
    		cvs.onmousemove = drawMouse;
    	}
    	
    	return false;
    }
    
    // Called whenever cursor position changes after drawing has started
    function stopDraw(e) {
    	e.preventDefault();
    	cvs.onmousemove = null;
    }
    
    function drawMouse(e) {
    	if (e.touches) {
    		// Touch Enabled
    		for (var i = 1; i <= e.touches.length; i++) {
    			var p = getCoords(e.touches[i - 1]); // Get info for finger i
    			cb_lastPoints[i] = drawLine(cb_lastPoints[i].x, cb_lastPoints[i].y, p.x, p.y);
    		}
    	}
    	else {
    		// Not touch enabled
    		var p = getCoords(e);
    		cb_lastPoints[0] = drawLine(cb_lastPoints[0].x, cb_lastPoints[0].y, p.x, p.y);
    	}
    	cb_ctx.stroke();
    	cb_ctx.closePath();
    	cb_ctx.beginPath();
    
    	return false;
    }
    
    // Draw a line on the canvas from (s)tart to (e)nd
    function drawLine(sX, sY, eX, eY) {
    	cb_ctx.moveTo(sX, sY);
    	cb_ctx.lineTo(eX, eY);
    	return { x: eX, y: eY };
    }
    
    // Get the coordinates for a mouse or touch event
    function getCoords(e) {
    	if (e.offsetX) {
    		return { x: e.offsetX, y: e.offsetY };
    	}
    	else if (e.layerX) {
    		return { x: e.layerX, y: e.layerY };
    	}
    	else {
    		return { x: e.pageX - cvs.offsetLeft, y: e.pageY - cvs.offsetTop };
    	}
    }

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    It seems that cvs has been assigned nothing other than null, even though properties are later on being set to that cvs variable, which is still null.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •