简体   繁体   English

如何制作画布动画比例(HTML5)

[英]How to make a canvas animation scale (HTML5)

I have an animation/canvas drawing where the pixels are hard coded and not relative. 我有一个动画/画布绘图,其中像素是硬编码的,而不是相对的。 How do I make it so that the canvas and the drawing/animation scales with the browser size? 如何使浏览器的大小和画布/绘图/动画缩放?

I thought of making the pixel values relative to the width and height of either the browser or the canvas but I don't know if it's a good idea. 我曾想过要根据浏览器或画布的宽度和高度来设置像素值,但是我不知道这是一个好主意。

Any tips? 有小费吗?

PS: I didn't post the code because it is almost 1000 lines long but if required I could post part of it. PS:我没有发布代码,因为它差不多有1000行,但是如果需要,我可以发布一部分。

Scale to fit, fill, or stretch 缩放以适合,填充或拉伸

Almost every device has a different display aspect ratio, and then each setup will use only part, or all of the available screen to display the window. 几乎每个设备的显示纵横比都不同,然后每个设置将仅使用部分或全部可用屏幕来显示窗口。

The only real choice you have is how to adapt to fit the available space. 您唯一真正的选择是如何适应可用空间。

As the previous answer points out you can get the available size with innerWidth, innerHeight. 正如前面的答案指出的那样,您可以使用innerWidth,innerHeight获得可用的大小。

So lets assume you have that as width and height. 因此,假设您具有宽度和高度。

 var width = innerWidth; // or canvas width
 var height = innerHeight; //

Default resolution 默认分辨率

I am assuming you app has a fixed aspect and an optimal resolution. 我假设您的应用具有固定的外观和最佳的分辨率。 To make it simple to adapt your app you need to keep the optimal resolution stored in code. 为了简化适应您的应用程序,您需要保持存储在代码中的最佳分辨率。

var defaultWidth = ?
var defaultHeight = ?

To adapt to displays you will need to get a scale that you set at the start of the app and adjust when the display size changes. 为了适应显示,您需要获得一个在应用程序开始时设置的比例,并在显示尺寸更改时进行调整。 You will also need the origin. 您还将需要原点。 the point where coordinate 0,0 is as for some solutions that will move. 坐标0,0与将要移动的某些解一样。

These values are all relative to the current display that app is rendering on. 这些值都是相对于应用正在渲染的当前显示而言的。 They will take you native coordinates and make them conform to the device pixels. 它们将获取您的原始坐标,并使它们与设备像素一致。

var displaySetting = {
    scaleX : ?,
    scaleY : ?,
    originX : ?,
    originY : ?,  
}

Fit, fill, or stretch 适合,填充或拉伸

The 3 most basic options. 3个最基本的选项。

  • Stretch to fit. 拉伸至适合。 You stretch the rendering to fit the display but lose the aspect. 您拉伸渲染以适合显示效果,但失去了外观。
  • Scale to fit. 缩放以适合。 You scale the rendering so that you maintain the aspect and all of the display is visible. 您可以缩放渲染,以保持外观,并且所有显示都是可见的。 Thought depending on the device you may have empty space on the sides or top and bottom. 根据设备的不同,可能会在侧面或顶部和底部留有空间。
  • Scale to fill. 缩放以填充。 You scale the rendering so that it fills the device screen but you may end up clipping some or the rendering on the sides or top and bottom. 您可以缩放渲染以使其充满设备屏幕,但最终可能会在侧面或顶部和底部修剪一些或渲染。

.

function getDisplayTransform(type, displaySetting){
    displaySetting = displaySetting || {};
    // first get scales
    if(type === "stretch"){
        displaySetting.scaleX = width / defaultWidth;
        displaySetting.scaleY = height / defaultHeight;
    } else if (type === "fit") {
        displaySetting.scaleX = Math.min(width / defaultWidth, height / defaultHeight);
        displaySetting.scaleY = displaySetting.scaleX;
    } else {  // type to fill
        displaySetting.scaleX = Math.max(width / defaultWidth, height / defaultHeight);        
        displaySetting.scaleY = displaySetting.scaleX;
    }
    // now that the scale is set get the location of the origin. which is 
    displaySetting.originX = width / 2 - defaultWidth * 0.5 * displaySetting.scaleX;
    displaySetting.originY = height / 2 - defaultHeight * 0.5 * displaySetting.scaleY;
    // Note that for scale to fill the origin may be of the display or canvas
    return displaySetting;
}

So now you just have to set the new coordinate system 所以现在您只需要设置新的坐标系

ctx.setTransform(
     displaySetting.scaleX,0,
     0,displaySetting.scaleY,
     displaySetting.originX,displaySetting.originY
);

So to fit with space to spare use getDisplayTransform("fit",displaySetting); 因此,为了腾出空间以使用getDisplayTransform("fit",displaySetting); or to fill use getDisplayTransform("fill",displaySetting); 或使用getDisplayTransform("fill",displaySetting); which will have some clipping top & bottom or left & right. 顶部和底部或左侧和右侧都有一些剪裁。

Once the transform is set you render as normal. 设置变换后,即可正常渲染。 You don't have to change the coordinates in your code, Line widths, font sizes, shadow offsets all remain the same as far as you code is concerned. 您不必更改代码中的坐标,就代码而言,线宽,字体大小,阴影偏移都保持不变。

Set the canvas width and height to window.innerWidth and window.innerHeight respectively. 将画布的widthheight分别设置为window.innerWidthwindow.innerHeight So, the code would be like (in JS): 因此,代码将类似于(在JS中):

var canvas = document.getElementById('my_canvas');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

Above code will only change the canvas size according to window width and height. 上面的代码只会根据窗口的宽度和高度更改画布大小。 To set the drawing/animation scale, you need to set its position/size dynamically (respect to the window width/height), not hard coded. 要设置绘图/动画比例,您需要动态设置其位置/大小(相对于窗口的宽度/高度),而不是硬编码。

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

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