Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

grid implementation html canvas

import { Shapes } from "./shapes";
import { Vector } from "./vector";

/**
 * Grid class
 * @class Grid
 * @classdesc Grid class
 * @param {number} width - width of the grid
 * @param {number} height - height of the grid
 * the class to create a virtual grid on the canvas with the given width and height for each cell
 */
export class Grid {
    public width: number
    public height: number
    public Shape : Shapes
    constructor(width: number, height: number) {
        this.width = width
        this.height = height
        this.Shape = new Shapes()
    }

    /**
     * @function draw
     * draw the grid on the canvas on the canvas in every frame
     * @memberof Grid
     */
    public draw() {
        var x, y;

        for (x = 0; x < window.innerWidth; x += this.width) {
            for (y = 0; y < window.innerHeight; y += this.height) {
                this.Shape.setStrokeStyle("#000000")
                this.Shape.setColor("#00000011")
                this.Shape.drawRectangle(new Vector(x, y), this.width, this.height)
            }
        }
    }

   // draw point on grid using it width and height
   /**
    * @function drawPoint
    * @param {Vector} point
    * draw a point on the grid according to the grid's cordinates not the canvas cordinates 
    * @memberof Grid
    */
    public drawPoint(point: Vector) {
        this.Shape.drawRectangle(new Vector(point.x * this.width, point.y * this.height), this.width, this.height)
    }

    /**
     * @function drawPoints
     * @param {Vector[]} points 
     * draw a list of points on the grid according to the grid's cordinates not the canvas cordinates
     * @memberof Grid
     */
    public drawPoints(points: Vector[]) {
        points.forEach(point => {
            this.Shape.drawRectangle(new Vector(point.x * this.width, point.y * this.height), this.width, this.height)
        })
    }

}
Comment

javascript draw canvas grid

var ctx = canvas.getContext('2d');
Comment

PREVIOUS NEXT
Code Example
Javascript :: How do I use for-loops js 
Javascript :: copy an array 
Javascript :: vue on page link or anchor 
Javascript :: plus operator javascript 
Javascript :: react faq 
Javascript :: Create array literal 
Javascript :: disadvantages of array 
Javascript :: jaascript loop 
Javascript :: quiz javascript 
Javascript :: switch statement 
Javascript :: higher order function 
Javascript :: how to get last element in array java scipt 
Javascript :: add to json object javascript 
Javascript :: find in javascript 
Javascript :: javascript debugging 
Javascript :: variables in javascript 
Javascript :: js compiler 
Javascript :: react-native-vector-icons 
Javascript :: typedjs 
Javascript :: router 
Javascript :: how to usestate in react 
Javascript :: replace all swear words using bookmarklet 
Javascript :: dockument anywhere click fucntion in js 
Javascript :: une expression de fonction en javascript 
Javascript :: changing parent function states in child function 
Javascript :: action cable nuxtjs 
Javascript :: popup react now ui 
Javascript :: JS urdsathdzygo8sdhurj.hdo78suij 
Javascript :: what is mdoe in math 
Javascript :: how to add theme attribute to :root 
ADD CONTENT
Topic
Content
Source link
Name
3+1 =