Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Graphic(width, height){
  2.   this.width  = width;
  3.   this.height = height;
  4.   this.canvas = null;
  5. }
  6.  
  7. Graphic.prototype.init = function(){
  8.   this.canvas        = document.createElement("canvas");
  9.   this.canvas.width  = this.width;
  10.   this.canvas.height = this.height;
  11.  
  12.   document.body.appendChild(this.canvas);
  13.   this.ctx = this.canvas.getContext("2d");
  14. };
  15.  
  16. Graphic.prototype.stick = function(width, points, colors){
  17.  
  18.   if(points.length != colors.length) console.error("Les couleurs ne sont pas toute déclarer !")
  19.  
  20.   var nbrGap   = points.length + 1;
  21.   var widthGap = (this.width - points.length * width) / nbrGap;
  22.   var gap      = 0;
  23.  
  24.   if(widthGap < 0){
  25.     console.error("Impossible d'affiche le graphique !");
  26.     return false;
  27.   }
  28.  
  29.   for(var i = 0; i < points.length; i++){
  30.     gap += widthGap;
  31.  
  32.     this.ctx.fillStyle = colors[i];
  33.     this.ctx.fillRect(gap, this.height - points[i], width, points[i]);
  34.  
  35.     gap += width;
  36.   }
  37. };
  38.  
  39. Graphic.prototype.hoverInfos = function(){
  40.   this.canvas.addEventListener("mousemove", function(e){
  41.     var x = e.clientX;
  42.     var y = e.clientY;
  43.     console.log("X : " + x +  " Y : " + y);
  44.   });
  45. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement