Advertisement
Guest User

Untitled

a guest
Dec 12th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. require('../styles/modules/boardCanvas.scss');
  2. import React, { Component, PropTypes } from 'react'
  3.  
  4. class BoardCanvas extends Component {
  5.  
  6.   componentDidMount() {
  7.     this.paint()
  8.   }
  9.  
  10.   componentDidUpdate(prevProp, prevState) {
  11.     //need to make this more leniant in the future in case if it wants a board refresh
  12.     if(prevProp && (!prevProp.boardProps.length || (this.props.isMobile !== prevProp.isMobile) || (prevProp.boardProps !== this.props.boardProps))){
  13.       this.paint()
  14.     }
  15.   }
  16.  
  17.   paint(){
  18.     console.log('painting')
  19.     const { isMobile } = this.props,
  20.           size = isMobile ? '320' : '640'
  21.     let context = this.refs.canvas.getContext("2d");
  22.     context.clearRect(0, 0, size, size);
  23.     context.fillStyle = this.props.colorHex.background;
  24.     context.rect(0,0, size, size);
  25.     context.fill();
  26.     this.drawBorder(context, size, size/16)
  27.     this.drawAllbackground(context, size/16)
  28.   }
  29.  
  30.   drawBorder(context, boardWidth, boxSize){
  31.     var p = 0;
  32.     for (var x = 0; x < boardWidth; x += boxSize) {
  33.         context.moveTo(x + p, p);
  34.         context.lineTo(x + p, boardWidth + p-5);
  35.     }
  36.     for (var x = 0; x < boardWidth; x += boxSize) {
  37.         context.moveTo(p, x + p);
  38.         context.lineTo(boardWidth + p-5, x + p);
  39.     }
  40.     context.lineWidth = 1;
  41.     context.strokeStyle = "grey";
  42.     context.stroke();
  43.   }
  44.  
  45.   drawAllbackground(context, size){
  46.     const { isMobile, boardProps } = this.props
  47.           self = this
  48.     boardProps.forEach(function(row, rowIndex){
  49.       row.forEach(function(tile, colIndex){
  50.         self.drawBackgroundX(context, size, colIndex*size, rowIndex*size)
  51.         self.drawWalls(context, size, colIndex*size, rowIndex*size, tile)
  52.       })
  53.     })
  54.   }
  55.  
  56.   drawBackgroundX(context, boxSize, x, y){
  57.     var colorHex = this.props.colorHex
  58.     /**
  59.      * 8 grey dots
  60.      * grey border square
  61.      * darkgrey minisquare
  62.      * yellow stripes moving diag..REALLY close to offwhite color, lil'yellow
  63.      *
  64.      * offwhite center sq...as huge border to block off missmatch from lines
  65.      */
  66.     context.beginPath();
  67.     context.moveTo(x+boxSize*.2, y+boxSize*.2);
  68.     context.lineTo(x+boxSize*.2, y+boxSize*.8);
  69.     context.lineTo(x+boxSize*.8, y+boxSize*.8);
  70.     context.lineTo(x+boxSize*.8, y+boxSize*.2);
  71.     context.closePath();
  72.     context.lineWidth = .25;
  73.     context.strokeStyle = '#ABADA0';
  74.     context.stroke();
  75.     //moveTo
  76.  
  77.     //dark grey border
  78.     //4 thick  diagonal dark grey strokes
  79.     //thick yellow-orange stroke:
  80.     context.beginPath();
  81.     context.strokeStyle = colorHex['xSquareY'];
  82.     context.lineWidth = boxSize*.23;
  83.     context.moveTo(x+boxSize*.33, y+boxSize*.33);
  84.     context.lineTo(x+boxSize*.75, y+boxSize*.75);
  85.     context.stroke();
  86.  
  87.     //thick diag dark grey strokes
  88.     context.beginPath();
  89.     context.strokeStyle = colorHex['xSquareG'];
  90.     context.lineWidth = boxSize*.09;
  91.     context.moveTo(x+boxSize*.33, y+boxSize*.33);
  92.     context.lineTo(x+boxSize*.75, y+boxSize*.75);
  93.     context.stroke();
  94.  
  95.     context.beginPath();
  96.     context.strokeStyle = colorHex['xSquareG'];
  97.     context.lineWidth = boxSize*.08;
  98.  
  99.     context.moveTo(x+boxSize*.25, y+boxSize*.47);
  100.     context.lineTo(x+boxSize*.50, y+boxSize*.73);
  101.     context.stroke();
  102.  
  103.     context.moveTo(x+boxSize*(1-.25), y+boxSize*(1-.47));
  104.     context.lineTo(x+boxSize*(1-.50), y+boxSize*(1-.73));
  105.     context.stroke();
  106.  
  107.     context.beginPath();
  108.     context.strokeStyle = colorHex['xSquareOverlay']
  109.     context.lineWidth = boxSize*.13;
  110.     context.moveTo(x+boxSize*.28, y+boxSize*.28);
  111.     context.lineTo(x+boxSize*.28, y+boxSize*.72);
  112.     context.lineTo(x+boxSize*.72, y+boxSize*.72);
  113.     context.lineTo(x+boxSize*.72, y+boxSize*.28);
  114.     context.closePath();
  115.     context.stroke();
  116.   }
  117.  
  118.   drawWalls(context, boxSize, x, y, tile){
  119.     if (tile.upWall){
  120.         this.drawOneWall(context, boxSize, x, y, "N");
  121.     }
  122.     if (tile.leftWall){
  123.         this.drawOneWall(context, boxSize, x, y, "W");
  124.     }
  125.     if (tile.downWall){
  126.         this.drawOneWall(context, boxSize, x, y, "S");
  127.     }
  128.     if (tile.rightWall){
  129.         this.drawOneWall(context, boxSize, x, y, "E");
  130.     }
  131.   }
  132.   drawOneWall(context, boxSize, x, y, dir){
  133.     context.moveTo(x,y);
  134.     context.beginPath();
  135.     //ld, lineDetail hash to draw lines relative to passed-in x and y
  136.     var ld = {
  137.         N:{
  138.             start:{x: 0, y:0},
  139.             end:{x: boxSize, y: 0}
  140.         },
  141.         S:{
  142.             start:{x:boxSize, y:boxSize},
  143.             end:{x:0, y:boxSize}
  144.         },
  145.         W:{
  146.             start:{x: 0, y:0},
  147.             end:{x:0, y:boxSize}
  148.         },
  149.         E:{
  150.             start:{x: boxSize, y: 0},
  151.             end:{x: boxSize, y: boxSize}
  152.         }
  153.     };
  154.     context.moveTo(x+ld[dir].start.x,y+ld[dir].start.y)
  155.     context.lineWidth = this.props.isMobile ? 5 : 7;
  156.  
  157.     context.lineTo(x+ld[dir].end.x,y+ld[dir].end.y);
  158.     context.strokeStyle = "black";
  159.     context.stroke();
  160.   }
  161.  
  162.   render() {
  163.     const { isMobile } = this.props,
  164.           size = isMobile ? '320' : '640'
  165.     return (
  166.       <div className = "canvas-container">
  167.         <canvas width = { size } height = { size } ref = "canvas" className = "board-canvas">
  168.         </canvas>
  169.       </div>
  170.     )
  171.   }
  172. }
  173.  
  174. BoardCanvas.defaultProps = {
  175.   colorHex: {
  176.       brown: '#6C4E2F',
  177.       R:'#F21018',
  178.       G: '#39CC36',
  179.       B: '#0C11CA',
  180.       Y: '#EFEB1D',
  181.       silverBorder    : '#72736D',
  182.       silverLight     : '#ABADA0', //tiny xSquare dots
  183.       lightYellow     : '#E8E549', //background
  184.       lightGrey       : '#66665D', //center walls
  185.       darkGrey        : '#51514B', //center square
  186.       background      : '#E7D4B0', //grey, orange tint
  187.       xSquareBorder   : '#ABADA0', //tiny xSquare dots
  188.       xSquareBg       : '#D4D7C7',//offwhite
  189.       xSquareOverlay  : '#ECEDE6', //light, offwhite
  190.       xSquareG        : '#DBDBDB',
  191.       xSquareY        : '#EBE0A6'
  192.   }
  193. };
  194.  
  195. export default BoardCanvas
  196.  
  197.  
  198. // WEBPACK FOOTER //
  199. // ./components/BoardCanvas.js
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement