简体   繁体   English

创建一个HTML5无限画布

[英]Creating a HTML5 infinite canvas

I'm trying to learn how to build an interactive canvas from scratch, but I'm having trouble trying to draw things outside the canvas' viewport (things that exceed canvas.width and canvas.height ). 我正在尝试学习如何从头开始构建交互式画布,但是在尝试在画布的视口之外绘制事物时遇到麻烦(超出canvas.widthcanvas.height )。 The goal is to have something like an infinite canvas where you can scroll and zoom and put things anywhere I want. 我们的目标是拥有一个无限的画布,您可以在其中滚动和缩放并将其放置在我想要的任何位置。

I figured out how to properly calculate the right insertion point when we zoomed out , the algorithm works like this: 我想通了如何正确地计算出正确的插入点时,我们缩小 ,算法是这样的:

see if the component should be added off the limits of the canvas; 见如果组件应该被添加画布的限制;
if so, transform the offset (x, y) adding the distance between the point and the edge of the canvas. 如果是这样,请变换偏移量(x,y),再加上点与画布边缘之间的距离。

I noticed that the event.pageX and event.pageY values are always given to me based on the width and height of the canvas, so if I'm zoomed out these values are, then, smaller than it should be (since I'm viewing more pixels). 我注意到event.pageXevent.pageY值始终基于画布的宽度和高度提供给我,因此,如果我缩小这些值,则它们将小于其应有的值(因为查看更多像素)。 The transform algorithm work as follows in JS: 转换算法在JS中的工作方式如下:

// pageX is 430, pageY is 480
// canvas has width=600 height=600
// scale is 0.6, meaning the canvas actually has size 360x360

var currentSize = canvas.width * scale;  // 360
pageX = canvas.width + (pageX - currentSize);
pageY = canvas.width + (pageY - currentSize);

Drawing on paper this logic seem to work, but the problem is I (apparently) can't draw outside canvas limits, so I'm unable to see the result. 在纸上画这种逻辑似乎行得通,但问题是我(显然)无法画出画布极限,所以我看不到结果。 Questions are: 问题是:

  • Is this logic correct? 这个逻辑正确吗?
  • Is there a way to achieve my goal? 有没有办法实现我的目标? (pointing right literature will be very appreciated) (指出正确的文献将不胜感激)
  • Is canvas the right tool to the job or I should use something else? 画布是工作的正确工具,还是我应该使用其他工具?

The complete example I'm using to learn can be found on this fiddle . 我正在学习的完整示例可以在此小提琴中找到。

UPDATE UPDATE

I had another idea to solve the problem: instead of drawing things outside the canvas, I simply translate my points to fit inside the canvas' limits proportionally and then apply scale to zoom in/out. 我有另一个想法可以解决该问题:我无需将其画在画布之外 ,而是简单地平移点以使其按比例适合画布的限制,然后应用缩放来放大/缩小。 Something like this: 像这样:

// canvas is 500x500
var points = [
    {text: 'Foo', x: 10, y: 10},
    {text: 'Bar', x: 854, y: 552},  // does not fit inside
    {text: 'Baz', x: 352, y: 440}
];

// The canvas can't show all these points, the ideal
// would be having a canvas of at least size 900x600,
// so I can use a rule of three to convert all points
// from this imaginary canvas to fit inside my 500x500

// in 900px, x=10
// in 500px, x=?

// hence, the formulas are `newX=x * 500 / 900` and `newY = y * 500 / 600`

var converted_points = [
  {text: 'Foo', x: 5.55, y: 8.33},
  {text: 'Bar', x: 474.44, y: 460},
  {text: 'Baz', x: 195.55, y: 366.66}
];

After that I suppose I would just need to scale/transform the canvas to do zooming. 之后,我想我只需要缩放/变换画布即可进行缩放。 Is that logic ok? 这样逻辑还可以吗?

You can use the library called TiledCanvas 您可以使用名为TiledCanvas的库
It provides interfaces to zoom and move. 它提供了缩放和移动界面。 And draw in an infinite space using all the canvas apis. 并使用所有画布API在无限的空间中绘制。

It does require that you tell where you are drawing. 它确实要求您告诉您要绘制的位置。

https://github.com/Squarific/TiledCanvas https://github.com/Squarific/TiledCanvas

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

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