Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "use strict";
- class SpriteImage
- {
- constructor(x, y, w, h, speed, clickable,img)
- {
- //posição e movimento
- this.xIni = x;
- this.yIni = y;
- this.x = x;
- this.y = y;
- this.width = w;
- this.height = h;
- this.speed = speed;
- //imagem
- this.img = img;
- //rato
- this.clickableIni = clickable;
- this.clickable = clickable;
- this.imgData = this.getimagedata(img);
- }
- draw(ctx)
- {
- ctx.drawImage(this.img, this.x, this.y, this.width, this.height);
- }
- clear(ctx)
- {
- ctx.clearRect(this.x, this.y, this.width, this.height);
- }
- reset(ev, ctx)
- {
- this.clear(ctx);
- this.x = this.xIni;
- this.y = this.yIni;
- this.clickable = this.clickableIni;
- }
- getimagedata(img){
- var canvas = document.createElement('canvas');
- canvas.width = this.width;
- canvas.height = this.height;
- var ctx = canvas.getContext("2d");
- ctx.drawImage(img,0,0,this.width,this.height);
- //console.log(ctx.getImageData(0,0,this.width,this.height));
- var re =ctx.getImageData(0,0,this.width,this.height);
- console.log(re);
- return re;
- }
- mouseOverBoundingBox(ev) //ev.target é a canvas
- {
- var mx = ev.offsetX; //mx, my = mouseX, mouseY na canvas
- var my = ev.offsetY;
- var x=mx-this.x;
- var y=my-this.y;
- var pos=y*(this.width-1)+x;
- console.log("X onde o rato se encontra"+mx);
- console.log("Y onde o rato se encontra"+my);
- console.log("Y onde o canvas se encontra"+this.y);
- console.log("X onde o canvas se encontra"+this.x);
- console.log("X "+x);
- console.log("Y "+y);
- console.log("posicaoo no array "+pos);
- console.log(this.imgData.data[(4*pos)-1]);
- if (mx >= this.x && mx <= this.x + this.width && my >= this.y && my <= this.y + this.height){
- if(this.imgData.data[(4*pos)-1]!=0){
- return true;
- }
- else
- return false;
- }
- else
- return false;
- }
- clickedBoundingBox(ev) //ev.target é a canvas
- {
- if (!this.clickable)
- return false;
- else
- return this.mouseOverBoundingBox(ev);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement