-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas.html
115 lines (99 loc) · 3.18 KB
/
canvas.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Ruby.wasm Quickstart</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="browser.script.iife.js"></script>
<style>
.spinner {
border: 4px solid rgba(0, 0, 0, 0.1);
width: 40px;
height: 40px;
border-radius: 50%;
border-left-color: #09f;
animation: spin 1s ease infinite;
}
@keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
</style>
<script type="text/ruby">
require 'js'
# Create a canvas element
window = JS.global
document = window.document
document.getElementById("spinner").style.display = "none"
canvas = document.createElement("canvas")
# Set canvas size
canvas.width = 400
canvas.height = 300
# Append the canvas to the document body
document.body.appendChild(canvas)
# Get the 2D drawing context of the canvas
context = canvas.getContext("2d")
def drawFace(context)
# Draw a yellow circle for the face
context.beginPath()
context.arc(200, 150, 100, 0, 2 * Math::PI)
context.fillStyle = "yellow"
context.fill()
# Draw the eyes
context.beginPath()
context.arc(150, 100, 15, 0, 2 * Math::PI)
context.arc(250, 100, 15, 0, 2 * Math::PI)
context.fillStyle = "black"
context.fill()
end
# Function to draw a smiley with a frown
def drawFrown(context)
drawFace(context)
# Draw the frown
context.beginPath()
context.arc(200, 180, 60, Math::PI, 2 * Math::PI)
context.lineWidth = 5
context.strokeStyle = "black"
context.stroke()
end
# Function to draw a smiley with a smile
def drawSmile(context)
drawFace(context)
# Draw the smile
context.beginPath()
context.arc(200, 180, 60, 0, Math::PI)
context.lineWidth = 5
context.strokeStyle = "black"
context.stroke()
end
# Initialize smile state
@smile = true
# Function to toggle smile/frown state and redraw
def toggleSmile(context)
canvas = context.canvas
if @smile
context.clearRect(0, 0, canvas.width, canvas.height)
drawFrown(context)
else
context.clearRect(0, 0, canvas.width, canvas.height)
drawSmile(context)
end
@smile = !@smile
end
# Initially draw a smiley with a smile
drawSmile(context)
# Toggle smile/frown every 1 second
window.setInterval(lambda { toggleSmile(context) }, 1000)
</script>
</head>
<body class="main">
<h3>
Canvas Example
</h3>
<div id="spinner" class="spinner"></div>
</body>
</html>