简体   繁体   English

如果我调整画布大小,我的createJS文本将变得模糊

[英]If I resize my canvas, my createJS text becames blurry

I'm using createJS to drawn inside the canvas. 我正在使用createJS在画布内绘制。 I have my canvas set to occupy the browser window maintaining aspect ratio using the resize() function. 我将画布设置为使用resize()函数保持浏览器窗口的纵横比。

This is the code: 这是代码:

    mytext = new createjs.Text("Lorem ipsum dolor sit amet 2","19px Calibri","#073949");
    mytext.x = 450
    mytext.y = 300;
    stage.addChild(mytext);  

    resize = function() {
                var canvas = document.getElementById('canvas');
                var canvasRatio = canvas.height / canvas.width;

                var windowRatio = window.innerHeight / window.innerWidth;
                var width;
                var height;

                if (windowRatio < canvasRatio) {
                    height = window.innerHeight - 35;
                    width = height / canvasRatio;
                } else {
                    width = window.innerWidth;
                    height = width * canvasRatio;
                }

                canvas.style.width = width + 'px';
                canvas.style.height = height + 'px';    
    }()

What happens is that the text gets blurry (decrease of quality) when the canvas resizes. 当画布调整大小时,会发生文本模糊(质量下降)的情况。

http://i.imgur.com/RQOSajs.png vs http://i.imgur.com/Xwhf5c5.png http://i.imgur.com/RQOSajs.pnghttp://i.imgur.com/Xwhf5c5.png

How can I solve this issue? 我该如何解决这个问题?

@Adam's answer is correct as far as scaling the canvas goes. 就缩放画布而言,@ Adam的答案是正确的。 You do NOT want to scale with CSS, as it will stretch your canvas instead of changing its pixel dimensions. 希望使用CSS缩放,因为它将拉伸画布而不是更改其像素尺寸。 Set the width and height of the canvas using JavaScript instead. 而是使用JavaScript设置画布的宽度和高度。

stage.canvas.width = window.innerWidth;
stage.canvas.height = window.innerHeight;

As you stated in your comment, this will only change the canvas size, and not reposition or scale your content. 如您在评论中所述,这只会更改画布的大小,而不会重新定位或缩放内容。 You will have to do this manually. 您将必须手动执行此操作。 This is fairly simple. 这很简单。 Generally, I recommend putting your "resize" listener in the JavaScript in your HTML file, rather than on a frame script. 通常,我建议将“调整大小”侦听器放在HTML文件中的JavaScript中,而不要放在框架脚本中。


First, determine the scale, based on the size of the window and the size of your content. 首先,根据窗口的大小和内容的大小确定比例。 You can use the exportRoot.nominalBounds.width and exportRoot.nominalBounds.height which is the bounds of the first frame. 您可以使用exportRoot.nominalBounds.widthexportRoot.nominalBounds.height ,它们是第一帧的边界。 If you want to scale something else, use its nominalBounds instead. 如果要缩放其他内容,请改用其nominalBounds

Note that nominalBounds is appended to all MovieClips exported from Flash/Animate. 请注意,nominalBounds附加到从Flash / Animate导出的所有MovieClip中。 If you enable multi-frame bounds, and want to use those, you will have to modify your approach. 如果启用了多框边界,并且要使用这些边界,则必须修改方法。

The main idea is to use the original, unscaled size of your contents. 主要思想是使用内容的原始未缩放大小。

var bounds = exportRoot.nominalBounds;
// Uses the larger of the width or height. This will "fill" the viewport.
// Change to Math.min to "fit" instead.
var scale = Math.max(window.innerWidth / bounds.width, window.innerHeight / bounds.height);
exportRoot.scaleX = exportRoot.scaleY = scale;

You can then center it if you want. 然后,您可以根据需要将其居中。

exportRoot.x = *window.innerWidth - bounds.width*scale)/2;
exportRoot.y = *window.innerHeight - bounds.height*scale)/2;

Here is a quick sample of a responsive canvas using a simple shape as the scaling contents: http://jsfiddle.net/lannymcnie/4yy08pax/ 这是使用简单形状作为缩放内容的响应式画布的快速示例: http : //jsfiddle.net/lannymcnie/4yy08pax/

Doing this with Flash/Animate CC export has come up a few times, so it is on my list of future EaselJS demos to include on createjs.com , and in the EaselJS GitHub . 使用Flash / Animate CC导出执行此操作已经有好几次了,因此我将在以后的EaselJS演示列表中找到它,包括在createjs.comEaselJS GitHub中

I hope this helps. 我希望这有帮助。

Since you are using CreateJS, you can simply resize the canvas, and scale the entire stage to redraw everything at the new size: 由于使用的是CreateJS,因此您只需调整画布的大小,并缩放整个阶段即可以新的大小重新绘制所有内容:

// just showing width to simplify the example:
var newWidth = 800;
var scale = newWidth/myCanvas.width;
myCanvas.width = newWidth;
myStage.scaleX = myStage.scaleY = scale;
myStage.update(); // draw at the new size.

Take a look at my jsfiddle : https://jsfiddle.net/CanvasCode/ecr7o551/1/ 看看我的jsfiddle: https ://jsfiddle.net/CanvasCode/ecr7o551/1/

Basically you just store the original canvas size and then use that to work out new positions and sizes 基本上,您只存储原始画布大小,然后使用它来计算新的位置和大小

html HTML

<canvas id="canvas" width="400" height="400">
    Canvas was unable to start up.
</canvas>

<button onclick="resize()">Click me</button>

javascript JavaScript的

var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');

var originalWidth = canvas.width;
var originalHeight = canvas.height;

render = function()
{
    context.fillStyle = "#DDD";
    context.fillRect(0,0, originalWidth * (canvas.width / originalWidth), originalHeight * (canvas.height / originalHeight));

    context.fillStyle = "#000";
    var fontSize = 48 * (canvas.width / originalWidth);
    context.font = fontSize+"px serif";
    context.fillText("Hello world", 100 * (canvas.width / originalWidth), 200 * (canvas.height / originalHeight));   
}

resize = function()
{
    canvas.width = 800;
    canvas.height = 600;
    render();
}

render();

The HTML5 canvas element works with two different sizes HTML5 canvas元素可以使用两种不同的大小

  • Visual size on screen, controlled via CSS, like you're setting with canvas.style.width/height 屏幕上的可视大小,通过CSS控制,就像您使用canvas.style.width / height进行设置一样
  • Size of pixel buffer for the drawing, controlled via numeric width and height pixel attributes on the canvas element. 通过画布元素上的数字宽度和高度像素属性控制图形的像素缓冲区的大小。

The browser will stretch the buffer to fit the size on screen, so if the two values are not 1:1 ratio text will look blurry. 浏览器将拉伸缓冲区以适合屏幕大小,因此,如果两个值不是1:1的比例,文本将显得模糊。

Try adding the following lines to your code 尝试将以下行添加到代码中

canvas.width = width;
canvas.height = height;   

I created a function to resize all the elements on the screen after resizing the canvas. 在调整画布大小后,我创建了一个函数来调整屏幕上所有元素的大小。 It saves the initial coordinates and scales for the elements with the original width of 900 px and then it changes them according to the current width ratio relative to the original width ratio. 它会保存原始宽度为900 px的元素的初始坐标和比例,然后根据当前宽度比例相对于原始宽度比例来更改它们。 The text isn't blurry/bad quality anymore. 文字不再是模糊/不良质量。

    resize = function() {
            var canvas = document.getElementById('canvas');
            var canvasRatio = canvas.height / canvas.width;

            var windowRatio = window.innerHeight / window.innerWidth;
            var width;
            var height;

            if (windowRatio < canvasRatio) {
                height = window.innerHeight;
                width = height / canvasRatio;
            } else {
                width = window.innerWidth;
                height = width * canvasRatio;
            }

            canvas.width = width;
            canvas.height = height;
            reziseElements();

        };


    reziseElements = function()
            {
                var canvrat = canvas.width / 900;

                 //Simplified
                stage.scaleX = stage.scaleY = canvrat;

                //Old Version
                /*for (i=0; i<stage.numChildren ;i++)
                {
                    currentChild = stage.getChildAt(i);

                    if (typeof currentChild.oscaleX == 'undefined')
                    {
                        currentChild.oscaleX = currentChild.scaleX;
                        currentChild.ox = currentChild.x;
                        currentChild.oy = currentChild.y;
                    }
                }   


                for (i=0; i<stage.numChildren ;i++)
                {
                    currentChild = stage.getChildAt(i);
                    currentChild.scaleX = currentChild.scaleY = currentChild.oscaleX * canvrat      
                    currentChild.x = currentChild.ox * canvrat
                    currentChild.y = currentChild.oy * canvrat
                }   */
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM