-
Notifications
You must be signed in to change notification settings - Fork 70
/
canvas-texture.html
92 lines (70 loc) · 1.7 KB
/
canvas-texture.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<!DOCTYPE html>
<html>
<head>
<title>A-Frame - Canvas Texture</title>
<script src="js/aframe-master-v1.3.0.min.js"></script>
</head>
<body>
<script>
AFRAME.registerComponent('draw-canvas', {
init: function()
{
this.canvas = document.querySelector("#mycanvas");
this.context = this.canvas.getContext('2d');
this.x = 200;
this.y = 100;
this.dx = 5;
this.dy = 3;
},
tick: function(t)
{
this.x += this.dx;
this.y += this.dy;
if (this.x > 512-50 || this.x < 0)
this.dx *= -1;
if (this.y > 512-50 || this.y < 0)
this.dy *= -1;
// clear canvas
this.context.fillStyle = "#8888FF";
this.context.fillRect(0,0, 512,512);
// draw rectangle
this.context.fillStyle = "#FF0000";
this.context.fillRect( this.x, this.y, 50, 50 );
// thanks to https://github.com/aframevr/aframe/issues/3936 for the update fix
let material = this.el.getObject3D('mesh').material;
if (!material.map)
return;
else
material.map.needsUpdate = true;
}
});
</script>
<a-scene>
<a-assets timeout="10000">
<img id="grid" src="images/border.png" />
<img id="sky" src="images/stars.jpg" />
<!-- texture sizes should be powers of 2 -->
<canvas id="mycanvas" width=512 height=512></canvas>
</a-assets>
<a-sky
rotation = "0 0 0"
color = "#FFFFFF"
material = "src: #sky">
</a-sky>
<a-plane
width="100" height="100"
position=" 0.00 0.00 0.00"
rotation="-90 0 0"
color="#888888"
material="src: #grid; repeat:100 100; transparent: true; opacity: 0.75">
</a-plane>
<a-plane
id = "canvas-display"
width = "6" height = "6"
position = "0 5 -10"
material = "shader: flat; src: #mycanvas;"
draw-canvas>
</a-plane>
</a-scene>
</body>
</html>