Solved

Getting HTML5 canvas signature to work in mobile

Posted on 2014-12-19
4
112 Views
Last Modified: 2015-12-02
Wondering how to get the signature part of this app

http://karrtransportation.com/employment/home/

to work when on a mobile phone, currently it just scrolls around when you try to sign.

Many Thanks!
0
Comment
Question by:Greg Alexander
4 Comments
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
0
 
LVL 42

Accepted Solution

by:
Rob Jurd, EE MVE earned 500 total points
Comment Utility
Here's a very simple demo that you should be able to easily incorporate into your site:

http://jsbin.com/cozaxu/1/edit?js,output

<canvas id="canvas" width="683" height="200" style="width: 683px; height: 200px; border: 1px solid rgb(0, 0, 0);">Canvas is not supported</canvas>

$(function() {
  var bDraw = false;
  var old = {};
  $('#canvas').on('touchstart', function(e) {
    e.preventDefault();
    //console.dir(e.originalEvent);
    old = {
      x: e.originalEvent.touches[0].clientX,
      y: e.originalEvent.touches[0].clientY
    };
    bDraw = true;

  });
  $('#canvas').on('touchend', function(e) {
    e.preventDefault();
    bDraw = false;
  });
  $('#canvas').on('touchmove', function(e) {
    e.preventDefault();
    var _from = old;
    var _to = {
      x: e.originalEvent.touches[0].clientX,
      y: e.originalEvent.touches[0].clientY      
    };
    old = _to;
    if (bDraw) {
      draw(_from, _to);
    }
  });

});

function draw(f, t) {
  var c = document.getElementById("canvas");
  var ctx = c.getContext("2d");
  ctx.moveTo(f.x,f.y);
  ctx.lineTo(t.x,t.y);
  ctx.stroke();
}

Open in new window

0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
In this tutorial viewers will learn how to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now