如何控制首先绘制哪个 Canvas 层(JCanvas)
在下面的代码中,我有两个画布层,一个文本和一个图像,我遇到的问题是控制首先绘制哪个层。当页面加载时,文本可能会绘制在图像上方或下方,这看起来很随机。有什么方法可以控制这种行为吗?
我的尝试:
{% extends "layout.html" %}
{% block body %}
<script>
window.onload = function(){
$("canvas").addLayer({
method: "drawImage",
source: '{{ url_for('static', filename='100MV-Floor-Plan.png') }}',
x: 700, y: 300,
})
$("canvas").addLayer({
method: "drawText",
strokeStyle: "C35817",
x:{{ room.xPos }}, y: {{ room.yPos }},
text: '{{ room.room_id }}',
align: "center",
baseline: "middle"
})
$("canvas").getLayer(0);
$("canvas").getLayer(1);
$("canvas").drawLayers();
};
</script>
<canvas width="1397" height="711"></canvas>
<h1>{{ room.current_user }}</h1>
<form method=POST>
Room ID: <input type="text" name="room_id"/><br />
<input type="submit" value="Click" class="button">
</form>
{% endblock %}
In the code below I have two Canvas layers one text and one image, the issue I'm having is controlling which layer draws first. When the page is loaded the text may draw above or blow the image, it seems pretty random. Is there a way I can control this behavior?
My attempt:
{% extends "layout.html" %}
{% block body %}
<script>
window.onload = function(){
$("canvas").addLayer({
method: "drawImage",
source: '{{ url_for('static', filename='100MV-Floor-Plan.png') }}',
x: 700, y: 300,
})
$("canvas").addLayer({
method: "drawText",
strokeStyle: "C35817",
x:{{ room.xPos }}, y: {{ room.yPos }},
text: '{{ room.room_id }}',
align: "center",
baseline: "middle"
})
$("canvas").getLayer(0);
$("canvas").getLayer(1);
$("canvas").drawLayers();
};
</script>
<canvas width="1397" height="711"></canvas>
<h1>{{ room.current_user }}</h1>
<form method=POST>
Room ID: <input type="text" name="room_id"/><br />
<input type="submit" value="Click" class="button">
</form>
{% endblock %}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
返回并再次查看 jcanvas 文档后,我找到了解决方案。通过使用drawImage()的“加载”回调函数,我可以在加载图像后调用drawText()。
更新的代码:
After going back and looking over the jcanvas doc's again I found a solution. By using drawImage()'s "load" call back function I'm able to call drawText() after the image has been loaded.
Updated Code: