首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HIDPI/Retina绘图?

HIDPI/Retina绘图?
EN

Stack Overflow用户
提问于 2013-11-26 15:37:49
回答 1查看 1.1K关注 0票数 4

在过去的一年里,我在bitbucket上见过3张票询问这个问题,但从未见过一个明确的答案。

这些票的给出了一些代码,但我不知道该代码属于哪里。

代码语言:javascript
复制
var devicePixelRatio = window.devicePixelRatio || 1,
backingStoreRatio = ctx.webkitBackingStorePixelRatio ||
        ctx.mozBackingStorePixelRatio ||
        ctx.msBackingStorePixelRatio ||
        ctx.oBackingStorePixelRatio ||
        ctx.backingStorePixelRatio || 1;

ratio = devicePixelRatio / backingStoreRatio;
if (devicePixelRatio !== backingStoreRatio) {
    var oldWidth = canvas.width;
    var oldHeight = canvas.height;
this.canvasOrigWidth = oldWidth;
this.canvasOrigHeight = oldHeight;
canvas.width = oldWidth * ratio;
canvas.height = oldHeight * ratio;

canvas.style.width = oldWidth + 'px';
canvas.style.height = oldHeight + 'px';

// now scale the context to counter
// the fact that we've manually scaled
// our canvas element
ctx.scale(ratio, ratio);
}

如何让JQPlot输出高分辨率的图形?

编辑1上面的代码似乎来自于这个网站

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-26 21:26:14

我根据问题中的例子找出了答案。

替换

代码语言:javascript
复制
this.initCanvas = function(canvas) {
    if ($.jqplot.use_excanvas) {
        return window.G_vmlCanvasManager.initElement(canvas);
    }
    return canvas;
}

使用

代码语言:javascript
复制
this.initCanvas = function(canvas) {
    if ($.jqplot.use_excanvas) {
        return window.G_vmlCanvasManager.initElement(canvas);
    }

    var cctx = canvas.getContext('2d');

    var canvasBackingScale = 1;
    if (window.devicePixelRatio > 1 && (cctx.webkitBackingStorePixelRatio === undefined || 
                                                cctx.webkitBackingStorePixelRatio < 2)) {
            canvasBackingScale = window.devicePixelRatio;
    }
    var oldWidth = canvas.width;
    var oldHeight = canvas.height;

    canvas.width = canvasBackingScale * canvas.width;
    canvas.height = canvasBackingScale * canvas.height;
    canvas.style.width = oldWidth + 'px';
    canvas.style.height = oldHeight + 'px';
    cctx.save();

    cctx.scale(canvasBackingScale, canvasBackingScale);

    return canvas;
};

这种方法可以在jquery.jqplot.js中的第290号线附近找到。

然后,如果您没有HIDPI或Retina显示器,但是有Mac,则可以使用Quartz Debug和System / display模拟HIDPI分辨率进行测试。下面是一个显示普通图形和替换代码的相同图形的复合屏幕截图。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20221461

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档