-
Notifications
You must be signed in to change notification settings - Fork 0
/
heartbeat.html
82 lines (71 loc) · 2.12 KB
/
heartbeat.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Heartbeat Monitor Animation with Ruby.wasm</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
canvas = document.createElement("canvas")
# Set canvas size
canvas.width = 800
canvas.height = 200
# Append the canvas to the document body
document.body.appendChild(canvas)
# Get the 2D drawing context of the canvas
ctx = canvas.getContext("2d")
# Initialize parameters for the heartbeat line
@x = 0
@height = canvas.height / 2
@step = 4 # Change in x, speed of the line
def draw_heartbeat(ctx)
canvas = ctx.canvas
if @x >= canvas.width
ctx.clearRect(0, 0, canvas.width, canvas.height)
@x = 0
end
# Draw the baseline
ctx.beginPath()
ctx.moveTo(@x, @height)
@x += @step
# Create a heartbeat effect
if rand < 0.1 # Random chance to create a spike
ctx.lineTo(@x, @height - 50) # Up
@x += @step
ctx.lineTo(@x, @height + 50) # Down
@x += @step
end
ctx.lineTo(@x, @height) # Back to baseline
ctx.strokeStyle = "red"
ctx.stroke()
end
# Update the heartbeat animation every 20 milliseconds
JS.global.setInterval(lambda { draw_heartbeat(ctx) }, 20)
</script>
</head>
<body>
<h3>Heartbeat Monitor Animation</h3>
</body>
</html>