HTML canvas - drawing disappear on resizing

前端 未结 7 1314
自闭症患者
自闭症患者 2020-12-03 17:08

I have created a basic shape in HTML canvas element which works fine.

The problem occurs when I resize the canvas, all the drawing in the canvas disappears. Is this

相关标签:
7条回答
  • 2020-12-03 18:06

    One another way is to use the debounce if you are concerned with the performance. It doesnt resize or redraw every position you are dragging. But it will resize only when the it is resized.

     // Assume canvas is in scope
     addEventListener.("resize", debouncedResize );
    
     // debounce timeout handle
     var debounceTimeoutHandle;
    
     // The debounce time in ms (1/1000th second)
     const DEBOUNCE_TIME = 100; 
    
     // Resize function 
     function debouncedResize () { 
        clearTimeout(debounceTimeoutHandle);  // Clears any pending debounce events
    
     // Schedule a canvas resize 
     debounceTimeoutHandle = setTimeout(resizeCanvas, DEBOUNCE_TIME);
     }
    
     // canvas resize function
     function resizeCanvas () { ... resize and redraw ... }
    
    0 讨论(0)
提交回复
热议问题