Client-Side Scripting
Client-Side Scripting
</body>
h1 p img
</html>
src alt
Live collection:
Changes in the DOM automatically update the collection
HTMLElement class:
outerText, innerText: text content of the element
and/or its descendants
HTMLElement class:
offsetLeft, offsetTop, offsetWidth,
offsetHeight: relative position/size of the outer border of
the current element to the inner border of the
offsetParent node
Examples:
image.src = "picture.png"; (standard attributes only)
image.setAttribute("src", "picture.png");
image.setAttribute("data-custom", "Nice day!");
Examples:
console.log(this === window); // true
var a = 10; // `const a`, `let a` not working
console.log(window.a); // 10
If the event is
Processes one
Mouse Moved not processed,
event at a time
it bubbles up
<body>
<p id="msg"></p>
</body>
</html>
➔ The element has not been constructed when the script is executed
<body>
<p id="msg"></p>
</body>
</html>
<body>
<p id="msg"></p>
<script>
const a = document.getElementById('msg');
a.innerText = 'Hello';
</script>
</body>
</html>
<body>
<p id="msg"></p>
<script src="my-script.js"></script>
</body>
</html>
<body>
<p id="msg"></p>
</body>
</html>
500px
(0,0) is the (500,0) is the top
top left corner right corner of
of the canvas 300px the canvas
(400, 100)
(100, 100)
ctx.beginPath();
ctx.moveTo(50, 50);
ctx.lineTo(100, 200);
ctx.lineTo(200, 100);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(50, 50);
ctx.lineTo(100, 200);
ctx.lineTo(200, 100);
ctx.closePath();
ctx.stroke();
lineJoin = "round"|"bevel"|"miter"
setLineDash(pattern)
Ex: setLineDash([3, 8, 10, 2])
...
ctx.closePath();
ctx.fill();
ctx.stroke();
...
ctx.closePath();
ctx.stroke();
ctx.fill();
Example:
ctx.beginPath();
ctx.arc(100, 75, 50, 0, Math.PI * 2);
ctx.clip();
ctx.fillStyle = 'blue';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = 'orange';
ctx.fillRect(0, 0, 100, 100);
img.onload = function() {
ctx.drawImage(img, 100, 50);
};
Question: Is it neccessary to set img.src before img.onload?
strokeText fillText
73 AC2070: Web Design & Programming
Đào Trung Kiên @ MICA Institute & Dept. of Comm. Eng., SEEE, Hanoi Univ. of Science and Technology
Shadows
Use the following properties:
shadowColor
shadowBlur
shadowOffsetX
shadowOffsetY
window.requestAnimationFrame(draw); ctx.fill();
}, 50); ctx.restore();
}
// first frame
draw();
Line commands:
M x y or m dx dy: move to (x, y) or relatively (dx, dy)
L x y or l dx dy: line to
H x or h dx: horizontal line
V y or v dy: vertical line
Z or z: close path
Example:
<path d="M 100 100 L 300 100 L 200 300 z"
fill="red" stroke="blue" stroke-width="3" />
SVG in SVG:
<image x="10" y="20" width="80" height="80"
href="external.svg" />
<svg ...>
<svg ...>
...
</svg>
</svg>
90 AC2070: Web Design & Programming
Đào Trung Kiên @ MICA Institute & Dept. of Comm. Eng., SEEE, Hanoi Univ. of Science and Technology
Opacity
Use opacity attribute/CSS property to make a whole shape opaque
Attn: For stroke and fill opacity, use stroke-opacity and fill-
opacity instead; for non-uniform opacity, use mask
Example:
<defs>
<linearGradient id="g1" x1="0%" y1="0%" x2="0" y2="100%">
<stop offset="0%" stop-color="skyblue" />
<stop offset="100%" stop-color="seagreen" />
</linearGradient>
</defs>
<rect x="0" y="0" width="100%" height="100%" fill="url(#g1)" />
<circle cx="50" cy="50" r="40" fill="black" />
<circle cx="150" cy="50" r="40" fill="black" opacity="0.3" />
Example:
<svg viewBox="0 0 100 100"
xmlns="http://www.w3.org/2000/svg">
<g fill="white" stroke="green" stroke-width="5">
<circle cx="40" cy="40" r="25" />
<circle cx="60" cy="60" r="25" />
</g>
</svg>
92 AC2070: Web Design & Programming
Đào Trung Kiên @ MICA Institute & Dept. of Comm. Eng., SEEE, Hanoi Univ. of Science and Technology
Transformations
Use transform attributes/CSS properties
translate(dx, dy), rotate(ang), scale(sx, sy), skewX(a), skewY(a)
matrix(m11, m12, m21, m22, dx, dy)
Example:
<svg viewBox="-40 0 150 100"
xmlns="http://www.w3.org/2000/svg">
<g fill="grey"
transform="rotate(-10 50 100) translate(-36 45.5)
skewX(40) scale(1 0.5)">
<path id="heart" d="M 10,30 A 20,20 0,0,1 50,30
A 20,20 0,0,1 90,30 Q 90,60 50,90
Q 10,60 10,30 z" />
</g>
<g filter="url(demoFilter)">
...
</g>
</svg>
<g filter="url(#f1)">
<circle cx="160" cy="120" r="40"
stroke="red" stroke-width="15" fill="none" />
<rect x="50" y="50" width="100" height="100"
stroke="none" fill="green" />
</g>
</svg>
<script>
const r = document.getElementById("r1");
let a = 0;
setInterval(() => {
r.setAttribute("transform", `rotate(${a})`);
a += 2;
}, 50);
</script>
<p>
<label for="name">Name:</label>
<input type="text" id="name" name="user_name">
</p>
<p>
<label for="mail">E-mail:</label>
<input type="email" id="mail" name="user_email">
</p>
<button>Register</button>
</form>
104 AC2070: Web Design & Programming
Đào Trung Kiên @ MICA Institute & Dept. of Comm. Eng., SEEE, Hanoi Univ. of Science and Technology
Adding Styles
form {
border: 1px solid #aaa;
border-radius: 5px;
width: fit-content;
max-width: 20rem;
padding: 1rem; input {
margin: auto; border: 1px solid #4b81e8;
} border-radius: 10rem;
padding: 5px;
h2 { }
margin: 0 0 1rem 0;
} button {
padding: 10px 20px;
.form-field { color: white;
margin: 5px 0; background: #4b81e8;
} border: transparent;
border-radius: 10rem;
label { font-weight: bold;
width: 5rem; display: block;
display: inline-block; margin: auto;
} }
105 AC2070: Web Design & Programming
Đào Trung Kiên @ MICA Institute & Dept. of Comm. Eng., SEEE, Hanoi Univ. of Science and Technology
<label>
The formal way to define a label for a form control
Labels are clickable too
Examples:
<label for="answ">Answer:</label>
<input type="text" id="answ" name="answer">
<label>Answer:
<input type="text" name="answer">
</label>
<label for="birthday">Birthday:</label><br/>
<input type="date" id="birthday" name="birthday" /><br/>
Examples:
input[type=text]:focus:read-only {
color: red;
}
document.querySelector(
'input[name="gender"]:checked').value
Examples:
<select id="simple" name="simple">
<option value="banana">Banana</option>
<option value="pear" selected>Pear</option>
<option value="lemon">Lemon</option>
</select>
<select id="multi" name="multi" multiple size="7">
...
</select>
Get selected options and values:
const simpleVal = document.getElementById('simple').value;