Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

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
Typescript :: test coverage techniques 
Typescript :: paths typescript 
Typescript :: nuxtServerInit nuxt 3 
Typescript :: nest js get request response 
Typescript :: world-times-newspaper-magazine-style-ghost-blog-theme 
Typescript :: Scroll, Position 
Typescript :: paginator 
Typescript :: across tab localstorage 
Typescript :: 8.1.3. Varying Data Types&para; Arrays 
Typescript :: angular minus date 
Typescript :: typeorm generated 
Typescript :: Cave Generator 
Typescript :: requests get with sign in 
Typescript :: uTorrent Default Download Folder - Linux 
Typescript :: open url with pacage.json 
Typescript :: run a code only once when two of the same gameobjects collide 
Typescript :: when 2 emits on a same chatroom at a time only one is working using socket.io 
Typescript :: What do HTTP requests and responses look like? 
Typescript :: nullable parameter typescript 
Typescript :: sum the digits in c 
Typescript :: typeorm versioncolumn 
Typescript :: TypeError: __cinit__() takes at least 2 positional arguments (0 given) 
Typescript :: classes and objects in python ppt 
Typescript :: serenity framework break form 
Typescript :: - laravel/ui[v3.2.0, ..., 3.x-dev] require illuminate/console ^8.0 - found illuminate/console[v8.0.0, ..., 8.x-dev] but these were not loaded, likely because it conflicts with another require. 
Typescript :: google sheets automatic update rook 
Typescript :: MInus points of exploration 
Typescript :: how to set up vuex with typescript 
Typescript :: vim show different parts of same file 
Typescript :: arranging array objects in custom order 
ADD CONTENT
Topic
Content
Source link
Name
5+9 =