Please explain this piece of code.

I came across this code when I was looking at html5 canvas examples. I pasted the code and I gave a comment in the place where I have doubts.

if(window.addEventListener) {

  window.addEventListener('load', function () {
  var canvas, context, tool;

  function init () {
    canvas = document.getElementById('imageView');
    if (!canvas) {
      alert('Error: I cannot find the canvas element!');
      return;
    }

    if (!canvas.getContext) {
      alert('Error: no canvas.getContext!');
      return;
    }

     context = canvas.getContext('2d');
    if (!context) {
      alert('Error: failed to getContext!');
      return;
    }

        tool = new tool_pencil();

    canvas.addEventListener('mousedown', ev_canvas, false);
    canvas.addEventListener('mousemove', ev_canvas, false);
    canvas.addEventListener('mouseup',   ev_canvas, false);
  }

   function tool_pencil () {
    var tool = this;
    this.started = false;


    this.mousedown = function (ev) {
        context.beginPath();
        context.moveTo(ev._x, ev._y);
        tool.started = true;
    };

      this.mousemove = function (ev) {
      if (tool.started) {
        context.lineTo(ev._x, ev._y);
        context.stroke();
      }
    };

       this.mouseup = function (ev) {
      if (tool.started) {
        tool.mousemove(ev);
        tool.started = false;
      }
    };
  }

    function ev_canvas (ev) {
    if (ev.layerX || ev.layerX == 0) { 
      ev._x = ev.layerX;
      ev._y = ev.layerY;
    } else if (ev.offsetX || ev.offsetX == 0) { 
      ev._x = ev.offsetX;
      ev._y = ev.offsetY;
    }

    //Please explain the follwing set of line
    var func = tool[ev.type];
    if (func) {
      func(ev);
    }
  }

  init();

}, false); }
+3
source share
3 answers

Combined with other answers, what it does is put all callbacks in one object. The result funcis tool.onmousedown, tool.onmouseupetc.

+3
source
//Please explain the follwing set of line
var func = tool[ev.type]; // Set func to the tool object member 
                          // named 'event.type' 
                          // Will set func to undefined if no function is 
                          // found in the tool object
if (func) {  // if a func was found then call it.
  func(ev);
}

Note that the hash object is toolused to store function references, not scalars, such as 1, "string", etc. A feature of Javascript is that you can create, save, and pass functions at runtime.

@ , an_obj ['unknown_key'] == undefined, null.

, foo ['a_key'] - foo.a_key - 'a_key' 'foo'.

, , Javascript . , , Hash, .

( , ). tool. :

  • started
  • mousedown, mouseup mousemove

, . , tool , . .

+1

It looks like he's trying to assign a "tool" type to a ev.typevariable func. This should be a function. He then checks whether the function exists (that is, if it was assigned or not), and calls it if it does, passing the variable evas a parameter.

0
source

Source: https://habr.com/ru/post/1773762/


All Articles