在下面的代码中,我有两个Canvas图层,一个文本和一个图像,我遇到的问题是控制哪个图层首先绘制。当页面加载时,文本可能会在上面绘制或吹动图像,这似乎很随机。有没有办法可以控制这种行为?
我的尝试:
{% 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 %}
答案 0 :(得分:4)
回过头来看看jcanvas doc后,我找到了解决方案。通过使用drawImage()的“load”回调函数,我可以在加载图像后调用drawText()。
更新代码:
1 {% extends "layout.html" %}
2 {% block body %}
3 <script>
4 window.onload = function(){
5 $("canvas").drawImage({
6 source: '{{ url_for('static', filename='100MV-Floor-Plan.png') }}',
7 x: 700, y: 300,
8 load: displayText
9 })
10
11 function displayText() {
12 $("canvas").drawText({
13 strokeStyle: "C35817",
14 x:{{ room.xPos }}, y: {{ room.yPos }},
15 text: '{{ room.room_id }}',
16 align: "center",
17 baseline: "middle"
18 });
19 };
20 };
21 </script>
22 <canvas width="1397" height="711"></canvas>
23 <h1>{{ room.current_user }}</h1>
24 <form method=POST>
25 Room ID: <input type="text" name="room_id"/><br />
26 <input type="submit" value="Click" class="button">
27 </form>
28 {% endblock %}