Advertisement
duarteguerreiro

Untitled

Mar 20th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "use strict";
  2.  
  3. class SpriteImage
  4. {
  5.     constructor(x, y, w, h, speed, clickable,img)
  6.     {
  7.         //posição e movimento
  8.         this.xIni = x;
  9.         this.yIni = y;
  10.         this.x = x;
  11.         this.y = y;
  12.         this.width = w;
  13.         this.height = h;
  14.         this.speed = speed;
  15.  
  16.         //imagem
  17.         this.img = img;    
  18.        
  19.         //rato
  20.         this.clickableIni = clickable;
  21.         this.clickable = clickable;
  22.  
  23.         this.imgData = this.getimagedata(img);     
  24.     }
  25.  
  26.  
  27.     draw(ctx)
  28.     {
  29.         ctx.drawImage(this.img, this.x, this.y, this.width, this.height);
  30.     }
  31.  
  32.  
  33.     clear(ctx)
  34.     {
  35.         ctx.clearRect(this.x, this.y, this.width, this.height);
  36.     }  
  37.  
  38.  
  39.     reset(ev, ctx)
  40.     {
  41.         this.clear(ctx);
  42.         this.x = this.xIni;
  43.         this.y = this.yIni;
  44.         this.clickable = this.clickableIni;
  45.     }
  46.     getimagedata(img){
  47.             var canvas = document.createElement('canvas');
  48.             canvas.width = this.width;
  49.             canvas.height = this.height;
  50.             var ctx = canvas.getContext("2d");
  51.             ctx.drawImage(img,0,0,this.width,this.height);
  52.             //console.log(ctx.getImageData(0,0,this.width,this.height));
  53.             var re =ctx.getImageData(0,0,this.width,this.height);
  54.             console.log(re);
  55.             return re;
  56.     }
  57.  
  58.  
  59.     mouseOverBoundingBox(ev) //ev.target é a canvas
  60.     {
  61.         var mx = ev.offsetX;  //mx, my = mouseX, mouseY na canvas
  62.         var my = ev.offsetY;
  63.         var x=mx-this.x;
  64.         var y=my-this.y;
  65.         var pos=y*(this.width-1)+x;
  66.         console.log("X onde o rato se encontra"+mx);
  67.         console.log("Y onde o rato se encontra"+my);
  68.         console.log("Y onde o canvas se encontra"+this.y);
  69.         console.log("X onde o canvas se encontra"+this.x);
  70.         console.log("X "+x);   
  71.         console.log("Y "+y);
  72.         console.log("posicaoo no array "+pos);
  73.         console.log(this.imgData.data[(4*pos)-1]);
  74.         if (mx >= this.x && mx <= this.x + this.width && my >= this.y && my <= this.y + this.height){
  75.             if(this.imgData.data[(4*pos)-1]!=0){
  76.                 return true;
  77.             }
  78.             else
  79.                 return false;
  80.         }
  81.         else
  82.             return false;
  83.     }
  84.  
  85.  
  86.     clickedBoundingBox(ev) //ev.target é a canvas
  87.     {
  88.         if (!this.clickable)
  89.             return false;
  90.         else
  91.             return this.mouseOverBoundingBox(ev);
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement