Home  >  Q&A  >  body text

javascript - How to eliminate jagged edges of canvas

Such as the title: I made a canvas graphic and the edges were jagged


I found one: Bicubic Interpolation interpolation algorithm, but I can’t understand it, please give me some advice
Orz!!

大家讲道理大家讲道理2720 days ago1044

reply all(2)I'll reply

  • phpcn_u1582

    phpcn_u15822017-05-31 10:43:27

    You can try doubling the canvas resolution first and take a look.
    Assume the display size is w * h:

    1. Set the width and height of canvas to 2w and 2h

    2. Set the width and height in the canvas style to w and h

    reply
    0
  • 大家讲道理

    大家讲道理2017-05-31 10:43:27

    I looked at some better plug-ins, such as echarts (Baidu Charts), but I couldn’t read more than 10,000 lines of code. Finally, I found a solution
    https://www.zhihu.com/questio...
    let width = canvas.width,height=canvas.height;
    if (window.devicePixelRatio) {

                canvas.style.width = width + "px";
                canvas.style.height = height + "px";
                canvas.height = height * window.devicePixelRatio;
                canvas.width = width * window.devicePixelRatio;
                ctx.scale(window.devicePixelRatio, window.devicePixelRatio);

    }
    I would like to share this with children’s shoes who encounter the same problem

    reply
    0
  • Cancelreply