Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

149
Visualizações
JS/TS proper structuring: canvas drawing from class, without class "knowing" about the canvas

I have a parent class, that has a <canvas> and a tool property.
(written in Typescript):

class CanvasHandler {
    canvas: HTMLCanvasElement;
    tool: Tool;

    constructor(canvas: HTMLCanvasElement, tool: Tool) {
        this.canvas = canvas;
        this.tool = tool;
    }
}

The tool is going to be an instance of the class Pen, that extends Tool:

interface IPen {
    size: number;
    color: string;
}

class Pen extends Tool implements IPen {
    size: number;
    color: string;

    constructor(size: number, color: string) {
        super(ToolNames.PEN, ToolDescriptions.PEN);

        this.size = size;
        this.color = color;
    }

    override onMouseMove(event: Event) {
       // DRAW CIRCLE TO CANVAS
    }

}

I have implemented an Observer on the <canvas>-Element, so now every time the mouse moves over the canvas, the method onMouseMove of CanvasHandler.tool gets executed. Now, every time the onMouseMove function gets called, I want to draw a circle to the <canvas>. I could provide a reference to the <canvas>, so that the class Tool "knows" the <canvas>, and ´onMouseMove´ I could override the drawing behaviour in Pen. However I wonder, if theres not a better way, to relocate the drawing functionality, so that the class Pen and Tool dont directly "know" about the <canvas> element.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda