-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path48.2_web_apis_3.js
79 lines (71 loc) · 2.11 KB
/
48.2_web_apis_3.js
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
/**
* ========================================================
* Web APIs - Part 3
* ========================================================
*/
/**
* ========================================================
* 1. Audio and Video API
* ========================================================
* The HTML5 Audio and Video APIs allow for playback of multimedia.
*/
const audio = new Audio("audio_file.mp3");
audio.play();
const video = document.querySelector("video");
video.play();
/**
* ========================================================
* 2. Canvas API
* ========================================================
* The Canvas API provides a way to draw 2D graphics.
*/
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");
ctx.fillStyle = "#FF0000";
ctx.fillRect(0, 0, 80, 80);
/**
* ========================================================
* 3. RequestAnimationFrame API
* ========================================================
* Used for creating smooth animations.
*/
function animate() {
// Animation code
requestAnimationFrame(animate);
}
animate();
/**
* ========================================================
* Nuances and Advanced Techniques
* ========================================================
*/
/**
* 1. Controlling Audio and Video Playback
* ---------------------------------------
* You can control the playback rates, volume, and other properties.
*/
audio.volume = 0.5;
audio.playbackRate = 1.5;
/**
* 2. Canvas Transformations
* -------------------------
* Canvas allows you to perform complex transformations like scaling and rotations.
* We'll talk more about canvas, in the next chapter.
*/
ctx.save();
ctx.rotate((Math.PI / 180) * 45);
ctx.fillRect(100, 0, 50, 50);
ctx.restore();
/**
* 3. Animating with RequestAnimationFrame
* ---------------------------------------
* It's best to use requestAnimationFrame over setInterval for smoother animations.
*/
let x = 0;
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillRect(x, 0, 50, 50);
x += 1;
requestAnimationFrame(animate);
}
animate();