[英]Set HTML canvas context via object
Up until now, I have set HTML canvas context the way in tutorials it is shown , like: 到现在为止,我已经在教程设置HTML画布上下文的方式则显示 ,如:
var canvas = document.getElementById("canvas"),
ctx = canvas.getContext("2d");
ctx.font = "15px calibri";
...
I was wondering whether it is possible to set the context of the canvas by passing in an object with the correct attributes to one of it's methods? 我想知道是否可以通过将具有正确属性的对象传递给其中一个方法来设置画布的上下文? For example: 例如:
var canvas = document.getElementById("canvas");
var context = {
font: "15px calibri",
...
);
canvas.setContext(context);
The reason for this is I am creating a custom canvas in angular. 原因是我正在以角度创建自定义画布。 For example, my controller
will get the <canvas>
element. 例如,我的controller
将获得<canvas>
元素。 A method in a service
will callback the context object to set for the canvas. service
的方法将回调上下文对象以为画布设置。
The custom canvas is to be converted to PNG and set as the icon
for a Marker on Google Maps. 自定义画布将转换为PNG并设置为Google地图上标记的icon
。 The marker is added to the maps in the service
: 标记将添加到service
的地图中:
addMarkerToMap: function(position, map, canvas) {
new google.maps.Marker({
position: position,
map: map,
icon: canvas.toDataURL("image/png")
});
}
So I am hoping to pass in the canvas
element into this fucntion (with it's context already set) and just set it's PNG equivalent as the Marker icon
. 所以我希望将canvas
元素传递给这个函数(已经设置了它的上下文) ,并将它的PNG等效设置为Marker icon
。
EDIT: 编辑:
I've now realised that other then just setting attributes (ie fillStyle
), I am invoking the contexts methods (ie beginPath()
). 我现在意识到除了设置属性(即fillStyle
)之外,我正在调用contexts方法(即beginPath()
)。
I think that this will make it much harder to accomplish what I am trying 我认为这将使我更难实现我的目标
Even though it looks like nonsence, after you mutate context of a canvas element, it is preserved: 即使它看起来像nonsence,在改变canvas元素的上下文之后,它仍然保留:
const canvas1 = document.getElementById('myCanvas');
const ctx = canvas1.getContext('2d');
ctx.font = '15px Calibri';
const canvas2 = document.getElementById('myCanvas');
console.log(canvas2.getContext('2d').font); // -> "15px Calibri"
So, in you case, it is safe to rely on context at any moment in future after you pass reference to canvas element to addMarkerToMap
function. 因此,在您的情况下,在将canvas元素的引用传递给addMarkerToMap
函数之后,将来随时依赖上下文是安全的。
Based on this answer - and the spec as it says, you possibly cannot replace the canvas context with another, as "Every canvas has what is called a primary context". 根据这个答案 - 以及它所说的规范,你可能无法用另一个替换画布上下文,因为“每个画布都有所谓的主要上下文”。
If possible, you could create a canvas programmatically and then append it to HTML as a child (or even cloning & replacing the old one at cases - if not too many replacements happening). 如果可能,您可以以编程方式创建画布,然后将其作为子项附加到HTML(甚至在案例中克隆和替换旧的 - 如果没有太多替换发生)。
You could for instance use jQuery to do something like: 例如,您可以使用jQuery执行以下操作:
// Just a new canvas, you could even clone the old one
var canvas1 = $('<canvas/>', {
id: 'canvas1',
height: 500,
width: 200,
font: '15 px'
});
document.body.appendChild(canvas1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.