简体   繁体   English

可缩放画布作为Google Maps JavaScript API中的叠加层

[英]Scalable canvas as overlay in Google Maps Javascript API

I have an image (9600x7200) that I am using as an overlay on a Google Maps enabled web page. 我有一张图像(9600x7200),正在用作启用了Google Maps的网页上的叠加层。 I anchor the SW and NW corners with latlng and the image scales correctly when I zoom the map. 我用latlng固定了SW和NW角,并且在缩放地图时可以正确缩放图像。 Now I want to create a second overlay that is an HTML5 canvas element, also sized 9600x7200, that sits on top of the image. 现在,我要创建第二个叠加层,该叠加层是HTML5画布元素,尺寸也为9600x7200,位于图片顶部。 I can do this but when I draw on the canvas, the scaling/positioning is all wrong. 我可以这样做,但是当我在画布上绘画时,缩放/定位都是错误的。 I used https://developers.google.com/maps/documentation/javascript/examples/overlay-simple as a template for both overlays, which sets the overlay element to 100% of the parent div, which is what is anchored to the map. 我使用https://developers.google.com/maps/documentation/javascript/examples/overlay-simple作为两个叠加层的模板,它将叠加层元素设置为父div的100%,这是固定在地图。 This works fine for a <img> , but not for an HTML5 canvas, which does not have an inherent size like an image does. 对于<img> ,这很好用,但是对于HTML5画布而言,效果并不好,HTML5画布没有图像的固有大小。 (you can set the width/height in CSS, but the underlying image has real dimensions that then scale to the CSS dimensions). (您可以在CSS中设置宽度/高度,但是基础图像具有实际尺寸,然后可以缩放为CSS尺寸)。

So, my question is: How do I create a drawable canvas size of 9600x7200, and anchor this to my map so it positions and scales to match the <img> overlay beneath it? 因此,我的问题是:如何创建9600x7200的可绘制画布大小,并将其锚定到我的地图上,以便其位置和缩放比例以匹配其下的<img>叠加层? Are there any code samples for this? 是否有任何代码示例?

Don't use CSS to resize your canvas, set it via the width and height attributes of the canvas element. 不要使用CSS来调整画布的大小,而是通过canvas元素的width和height属性进行设置。 If you draw on a canvas (let's say it's stretched to twice it's normal size for simplicity) both it's size and position will be stretched as well. 如果您在画布上绘画(为简单起见,它被拉伸到正常大小的两倍),那么它的大小和位置也会被拉伸。 So if you try to change the color of a pixel at x: 10, y: 10, you'll get a 2x2 (approximately) square at x: 20, y: 20. 因此,如果您尝试更改像素在x:10,y:10处的颜色,则会在x:20,y:20处得到一个2x2(大约)的正方形。

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

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