Advertisement
Guest User

Untitled

a guest
May 19th, 2012
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var platno, obsah, tool;
  2.  
  3. function init () {
  4.     // definovane platno a obsah platna
  5.     platno = document.getElementById('platno');
  6.     obsah = platno.getContext('2d');
  7.  
  8.     // vytvorenie pera
  9.     tool = new tool_pencil();
  10.  
  11.     // eventlistenery pre pohyby mysou a dotyky prsta
  12.     platno.addEventListener('mousedown', kresli, false);
  13.     platno.addEventListener('mousemove', kresli, false);
  14.     platno.addEventListener('mouseup',   kresli, false);
  15.     platno.addEventListener('touchstart', kresli, false);
  16.     platno.addEventListener('touchmove', kresli, false);
  17.     platno.addEventListener('touchend',   kresli, false);
  18.  
  19.     // zabrani scroolovaniu na platne, aby sa pri kresleni web nehybal
  20.     document.getElementById('platno').addEventListener('touchmove',function(event){
  21.         event.preventDefault();
  22.     },false);
  23. }
  24.  
  25. // nastroj na kreslenie
  26. function tool_pencil () {
  27.     var tool = this;
  28.     this.started = false;
  29.  
  30.     // ak sa drzi stlacene tlacidlo mysi, drzi prst na displayi,
  31.     // vykona sa funkcia a zacne sa kreslit ciara
  32.     this.touchstart = this.mousedown = function (event) {
  33.         obsah.beginPath();
  34.         obsah.moveTo(event._x, event._y);
  35.         tool.started = true;
  36.     };
  37.    
  38.     // funkcia sa vola ked sa pohybuje mysou, prstom
  39.     this.touchmove = this.mousemove = function (event) {
  40.         if (tool.started) {
  41.  
  42.             // male vyhladenie
  43.             var xc = (event._x + event._x+1) / 2;
  44.             var yc = (event._y + event._y+1) / 2;
  45.  
  46.             obsah.strokeStyle = "#df4b26";
  47.             obsah.lineWidth=2;
  48.             obsah.lineJoin="round";
  49.             obsah.lineCap = 'round';
  50.             obsah.quadraticCurveTo(event._x, event._y,xc, yc);
  51.             obsah.stroke();
  52.         }
  53.     };
  54.  
  55.     // sa zavola ak sa pusti mys alebo odtiahne prst
  56.     this.touchend = this.mouseup = function (event) {
  57.         if (tool.started) {
  58.             tool.mousemove(event);
  59.             tool.started = false;
  60.         }
  61.     };
  62.    
  63. }
  64.  
  65. // Iba event handler. Zistuje sa relativna pozicia prstu/mysi
  66. function kresli (event) {
  67.  
  68.     // Zistuje, ci zariadenie podporuje dotyk, vracia 0/1
  69.     function je_dotyk() {
  70.         return !!('ontouchstart' in window) ? 1 : 0;
  71.     }
  72.  
  73.     // Ak je dotykove zariadenie, suradnice sa zistia podla
  74.     // aktualnej polohy prsta, inak poloha kurzoru
  75.     if (je_dotyk() == 1) {
  76.     event._x = event.targetTouches[0].pageX-this.offsetLeft;
  77.     event._y = event.targetTouches[0].pageY-this.offsetTop;
  78.     }
  79.     else if (event.offsetX || event.offsetX == 0) {
  80.     event._x = event.offsetX;
  81.     event._y = event.offsetY;
  82.     }
  83.  
  84.     // Zavola sa event handler tool, cize nastroj(ceruzka)
  85.     var func = tool[event.type];
  86.     if (func) {
  87.         func(event);
  88.     }
  89. }
  90.  
  91. //inicializacia platna a event handlerov
  92. init();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement