Canvas drawing and Retina display: doable?

前端 未结 6 1370
北荒
北荒 2020-12-15 06:13

Working with phoneGap implementing drawing with Canvas. The catch we\'ve run into is that canvas expects specific pixel dimensions. This is fine except that the iPhone 4\'s

6条回答
  •  天涯浪人
    2020-12-15 06:51

    I sat with the same problem last week and discovered how to solve it -

    var canvas = document.querySelector('canvas');
    var ctx = canvas.getContext('2d');
    
    if (window.devicePixelRatio > 1) {
        var canvasWidth = canvas.width;
        var canvasHeight = canvas.height;
    
        canvas.width = canvasWidth * window.devicePixelRatio;
        canvas.height = canvasHeight * window.devicePixelRatio;
        canvas.style.width = canvasWidth;
        canvas.style.height = canvasHeight;
    
        ctx.scale(window.devicePixelRatio, window.devicePixelRatio);
    }
    

    Full code on gist, demo on jsfiddle

提交回复
热议问题