@@ -1226,8 +1226,8 @@ Using JavaScript, we'll add event listeners to dynamically update the form.
12261226``` html
12271227<script >
12281228document .getElementById (' choice' ).addEventListener (' change' , function () {
1229- var value = this .value ;
1230- var additionalFields = document .getElementById (' additionalFields' );
1229+ const value = this .value ;
1230+ const additionalFields = document .getElementById (' additionalFields' );
12311231
12321232 additionalFields .innerHTML = ' ' ; // Clear existing fields
12331233
@@ -2000,8 +2000,8 @@ On such an element, with the help of JavaScript, we can draw, for example, a cir
20002000
20012001``` javascript
20022002< script>
2003- var c = document .getElementById (" myCanvas" );
2004- var ctx = c .getContext (" 2d" );
2003+ const c = document .getElementById (" myCanvas" );
2004+ const ctx = c .getContext (" 2d" );
20052005ctx .beginPath ();
20062006ctx .arc (95 , 50 , 40 , 0 , 2 * Math .PI );
20072007ctx .stroke ();
@@ -2037,8 +2037,8 @@ function getLocation() {
20372037}
20382038
20392039function showPosition (position ) {
2040- var latitude = position .coords .latitude ;
2041- var longitude = position .coords .longitude ;
2040+ const latitude = position .coords .latitude ;
2041+ const longitude = position .coords .longitude ;
20422042 document .getElementById (" demo" ).innerHTML = " Latitude: " + latitude + " <br>Longitude: " + longitude;
20432043}
20442044 </script >
@@ -2128,7 +2128,7 @@ function allowDrop(event) {
21282128
21292129function drop (event ) {
21302130 event .preventDefault ();
2131- var data = event .dataTransfer .getData (" text" );
2131+ const data = event .dataTransfer .getData (" text" );
21322132 event .target .appendChild (document .getElementById (data));
21332133}
21342134 </script >
@@ -4004,9 +4004,17 @@ alert("Username: " + username);
40044004
40054005The Canvas API provides a powerful set of drawing and graphics capabilities, allowing developers to create dynamic and interactive graphics directly within the browser. With the Canvas API, developers can draw shapes, paths, text, images, and even perform animations and transformations. This API is commonly used for creating charts, graphs, games, image editing tools, and other visually rich content.
40064006
4007+ The ` <canvas> ` element is a powerful tool for creating graphics and animations directly within a web page.
4008+
4009+ ``` html
4010+ <canvas id =" myCanvas" width =" 400" height =" 200" style =" border :1px solid #000000 ;" ></canvas >
4011+ ```
4012+
4013+ On such an element, with the help of JavaScript, we can draw, for example, a rectangle, and a circle.
4014+
40074015``` javascript
4008- var canvas = document .getElementById (" myCanvas" );
4009- var ctx = canvas .getContext (" 2d" );
4016+ const canvas = document .getElementById (" myCanvas" );
4017+ const ctx = canvas .getContext (" 2d" );
40104018
40114019// Draw a red rectangle
40124020ctx .fillStyle = " red" ;
@@ -4019,6 +4027,12 @@ ctx.fillStyle = "blue";
40194027ctx .fill ();
40204028```
40214029
4030+ [ ![ Edit 110-Canvas API] ( https://codesandbox.io/static/img/play-codesandbox.svg )] ( https://codesandbox.io/p/sandbox/110-canvas-api-lgtkpg )
4031+
4032+ - [ ^ 110 ] CodeSandbox: Canvas API.
4033+
4034+ [ ^ 110 ] :[ CodeSandbox: Canvas API.] ( https://lgtkpg.csb.app/ ) , last access: October 1, 2024.
4035+
40224036- The code above demonstrates how to use the Canvas API to draw shapes on a canvas element.
40234037- First, a canvas context (` 2d ` ) is obtained from the canvas element.
40244038- Then, a red rectangle and a blue circle are drawn using ` fillRect() ` and ` arc() ` methods, respectively.
@@ -4030,17 +4044,31 @@ The Web Workers API enables concurrent execution of scripts in background thread
40304044
40314045``` javascript
40324046// Create a new web worker
4033- var worker = new Worker (" worker.js" );
4047+ const worker = new Worker (" worker.js" );
40344048
40354049// Handle messages from the worker
40364050worker .onmessage = function (event ) {
4037- console . log (" Message from worker: " + event .data );
4051+ alert (" Message from worker: " + event .data );
40384052};
40394053
40404054// Send a message to the worker
40414055worker .postMessage (" Hello from main thread!" );
40424056```
40434057
4058+ Additionally, a simple ` worker.js ` file:
4059+
4060+ ``` javascript
4061+ self .addEventListener (" message" , (e ) => {
4062+ self .postMessage (e .data );
4063+ });
4064+ ```
4065+
4066+ [ ![ Edit 111-Web Workers] ( https://codesandbox.io/static/img/play-codesandbox.svg )] ( https://codesandbox.io/p/sandbox/110-canvas-api-lgtkpg )
4067+
4068+ - [ ^ 111 ] CodeSandbox: Web Workers.
4069+
4070+ [ ^ 111 ] :[ CodeSandbox: Web Workers.] ( https://lgtkpg.csb.app/ ) , last access: October 1, 2024.
4071+
40444072- The code above demonstrates how to use web workers to perform tasks in background threads.
40454073- First, a new web worker is created by passing the path to a worker script (` worker.js ` ).
40464074- Event listeners are added to handle messages sent from the worker (` onmessage ` ) and errors (` onerror ` ).
0 commit comments