Categories
Web development

HTML Canvas Line Styles

HMTL Canvas Line Styles

lineCap

The HTML canvas lineCap property sets or returns the style of the end caps for a line.

Syntax:

context.lineCap="butt|round|square";

butt (default) – flat edges.

round – round edges.

square – square edges.

Example:

<!DOCTYPE html>
<html>
<body>

<canvas id="lineCap" width="240" height="150" style="border:1px solid #333;">
</canvas>

<script>
var c = document.getElementById("lineCap");
var ctx = c.getContext("2d");

ctx.beginPath();
ctx.lineWidth = 8;
ctx.lineCap = "butt";
ctx.moveTo(30, 20);
ctx.lineTo(200, 20);
ctx.stroke();

ctx.beginPath();
ctx.lineCap = "round";
ctx.moveTo(30, 70);
ctx.lineTo(200, 70);
ctx.stroke();

ctx.beginPath();
ctx.lineCap = "square";
ctx.moveTo(30, 120);
ctx.lineTo(200, 120);
ctx.stroke();
</script>

</body>
</html>

Output:


lineJoin

The HTML canvas lineJoin property sets or returns the type of corner created, when two lines meet.

Syntax:

context.lineJoin="miter|round|bevel";

miter (default) – a sharp corner.

round – a rounded corner.

bevel – a beveled corner.

Example:

<!DOCTYPE html>
<html>
<body>

<canvas id="roundedCorener" width="200" height="200" style="border:1px solid #333;">
</canvas>

<script>
var c = document.getElementById("roundedCorener");
var ctx = c.getContext("2d");
ctx.beginPath();
ctx.lineWidth = 10;
ctx.lineJoin = "round";
ctx.moveTo(20, 20);
ctx.lineTo(150, 75);
ctx.lineTo(20, 150);
ctx.stroke();
</script>

</body>
</html>

Output:


lineWidth

The HTML canvas lineWidth property sets or returns the current line width, in pixels.

Syntax:

context.lineWidth=number;

number – line width, in pixels.

Example:

<!DOCTYPE html>
<html>
<body>

<canvas id="lineWidth" width="200" height="200" style="border:1px solid #333;"></canvas>

<script>
var c = document.getElementById("lineWidth");
var ctx = c.getContext("2d");
ctx.lineWidth = 8;
ctx.strokeRect(50, 50, 100, 100);
</script>

</body>
</html>

Output:


miterLimit

The HTML canvas miterLimit property sets or returns the maximum miter length.

The miter length is the distance between the inner corner and the outer corner where two lines meet.

Syntax:

context.miterLimit=number;

number – a positive number that defines the maximum miter length.

Example:

<!DOCTYPE html>
<html>
<body>

<canvas id="miterLimit" width="200" height="150" style="border:1px solid #333;"></canvas>

<script>
var c = document.getElementById("miterLimit");
var ctx = c.getContext("2d");
ctx.lineWidth = 8;
ctx.lineJoin = "miter";
ctx.miterLimit = 5;
ctx.moveTo(20, 20);
ctx.lineTo(50, 27);
ctx.lineTo(20, 34);
ctx.stroke();
</script>

</body>
</html>

Output:


Enjoy coding!

Categories
Web development

HTML Canvas Shadows

HTML5 Canvas Shadows

To create shadow in HTML canvas use the shadowColor property together with the shadowBlur property.

The HTML canvas shadowColor property sets or returns the color to use for shadows.

Syntax:

context.shadowColor=color;

The HTML canvas shadowBlur property sets or returns the blur level for shadows.

Syntax:

context.shadowBlur=number;

Example:

<!DOCTYPE html>
<html>
<body>

<canvas id="shadow-blur" width="300" height="180" style="border:1px solid #333;">
</canvas>

<script>
var c = document.getElementById("shadow-blur");
var ctx = c.getContext("2d");
ctx.shadowBlur = 30;
ctx.fillStyle = "#2a9d8f";

ctx.shadowColor = "black";
ctx.fillRect(30, 40, 100, 100);

ctx.shadowColor = "#264653";
ctx.fillRect(170, 40, 100, 100);
</script>

</body>
</html>

Output:


To adjust the shadow use the shadowOffsetX and shadowOffsetY properties.

The HTML canvas shadowOffsetX property sets or returns the horizontal distance of the shadow from the shape.

Syntax:

context.shadowOffsetX=number;

Example:

<!DOCTYPE html>
<html>
<body>

<canvas id="offset-x" width="200" height="200" style="border:1px solid #333;">
</canvas>

<script>
var c = document.getElementById("offset-x");
var ctx = c.getContext("2d");
ctx.shadowBlur = 15;
ctx.shadowOffsetX = 25;
ctx.shadowColor = "black";
ctx.fillStyle= "#2a9d8f";
ctx.fillRect(50, 50, 100, 100);
</script>

</body>
</html>

Output:


The HTML canvas shadowOffsetY property sets or returns the vertical distance of the shadow from the shape.

Example:

<!DOCTYPE html>
<html>
<body>

<canvas id="offset-y" width="200" height="200" style="border:1px solid #333;">
</canvas>

<script>
var c = document.getElementById("offset-y");
var ctx = c.getContext("2d");
ctx.shadowBlur = 15;
ctx.shadowOffsetY = 25;
ctx.shadowColor = "black";
ctx.fillStyle= "#2a9d8f";
ctx.fillRect(50, 50, 100, 100);
</script>

</body>
</html>

Output:


Enjoy coding!

Categories
Web development

HTML Canvas Gradients

The HTML canvas gradient can be used to fill shapes (like circles, squares, rectangles, lines, text, etc.).

HTML5 canvas gradients

You can create two different types of gradients:

createLinearGradient(x,y,x1,y1) – creates a linear gradient.

Syntax:

context.createLinearGradient(x0,y0,x1,y1);

Example:

<!DOCTYPE html>
<html>
<body>

<canvas id="square-3" width="200" height="200" style="border:1px solid #333;">
</canvas>

<script>
var c = document.getElementById("square-3");
var ctx = c.getContext("2d");
var createGradient = ctx.createLinearGradient(0, 0, 150, 0);
createGradient.addColorStop(0, "#e76f51");
createGradient.addColorStop(0.5, "#f4a261");
createGradient.addColorStop(1, "#e9c46a");
ctx.fillStyle = createGradient;
ctx.fillRect(50, 50, 100, 100);
</script>

</body>
</html>

Output:


Where:

x0 – the x-coordinate of the start point of the gradient.

y0 – the y-coordinate of the start point of the gradient.

x1 – the x-coordinate of the endpoint of the gradient.

y1 – the y-coordinate of the endpoint of the gradient.

createRadialGradient(x,y,r,x1,y1,r1) – creates a radial/circular gradient.

Syntax:

context.createRadialGradient(x0,y0,r0,x1,y1,r1);

Example:

<!DOCTYPE html>
<html>
<body>

<canvas id="radialGradient" width="200" height="200" style="border:1px solid #333;">
</canvas>

<script>
var c = document.getElementById("radialGradient");
var ctx = c.getContext("2d");
var grd = ctx.createRadialGradient(100,100,30, 100,100,70);
grd.addColorStop(0, "#e76f51");
grd.addColorStop(1, "#e9c46a");
ctx.fillStyle = grd;
ctx.fillRect(50, 50, 100, 100);
</script>

</body>
</html>

Output:


x0 – the x-coordinate of the starting circle of the gradient.

y0 – the y-coordinate of the starting circle of the gradient.

r0 – the radius of the starting circle.

x1 – the x-coordinate of the ending circle of the gradient.

y1 – the y-coordinate of the ending circle of the gradient.

r1 – the radius of the ending circle.

To define different colors add two or more color stops. To create the color stops use the addColorStop():

Syntax:

gradient.addColorStop(stop,color);

Example:

<!DOCTYPE html>
<html>
<body>

<canvas id="colorStops" width="200" height="200" style="border:1px solid #333;"></canvas>

<script>
var c = document.getElementById("colorStops");
var ctx = c.getContext("2d");
 
var grd = ctx.createLinearGradient(0, 0, 170, 0);
grd.addColorStop(0, "black");
grd.addColorStop("0.3", "magenta");
grd.addColorStop("0.5", "blue");
grd.addColorStop("0.6", "green");
grd.addColorStop("0.8", "yellow");
grd.addColorStop(1, "red");
 
ctx.fillStyle = grd;
ctx.fillRect(50, 50, 100, 100); 
</script>

</body>
</html>

Output:


Enjoy coding!

Categories
Web development

HTML canvas strokeStyle Property

HTML canvas strokeStyle Property

The HTML canvas strokeStyle property defines the color, gradient, or pattern used for strokes.

value: color

Draw a square with a green stroke:

<!DOCTYPE html>
<html>
<body>

<canvas id="drawStroke" width="200" height="200" style="border:1px solid #333;"></canvas>

<script>
var c = document.getElementById("drawStroke");
var ctx = c.getContext("2d");
ctx.strokeStyle = "#2a9d8f";
ctx.strokeRect(50, 50, 100, 100);
</script>

</body>
</html>

Output:


value: gradient

Draw a square with a gradient stroke:

<!DOCTYPE html>
<html>
<body>

<canvas id="gradientStroke" width="200" height="200" style="border:1px solid #333;">
</canvas>

<script>
var c = document.getElementById("gradientStroke");
var ctx = c.getContext("2d");

var gradient = ctx.createLinearGradient(0, 0, 150, 0);
gradient.addColorStop("0", "#e9c46a");
gradient.addColorStop("0.5", "#2a9d8f");
gradient.addColorStop("1.0", "#e76f51");

// Fill with gradient
ctx.strokeStyle = gradient;
ctx.lineWidth = 5;
ctx.strokeRect(50, 50, 100, 100);
</script>

</body>
</html>

Output:


Write the text with a gradient stroke:

<!DOCTYPE html>
<html>
<body>

<canvas id="gradienttextStroke" width="200" height="200" style="border:1px solid #333;">
</canvas>

<script>
var c = document.getElementById("gradienttextStroke");
var ctx = c.getContext("2d");

ctx.font = "50px Arial";

// Create gradient
var gradient = ctx.createLinearGradient(0, 0, c.width, 0);
gradient.addColorStop("0", "#e76f51");
gradient.addColorStop("0.5", "#2a9d8f");
gradient.addColorStop("1.0", "#e9c46a");

// Fill with gradient
ctx.strokeStyle = gradient;
ctx.strokeText("Text", 50, 110);
</script>

</body>
</html>

Output:

Enjoy coding!

Categories
Web development

HTML canvas fillStyle Property

HTML canvas fillStyle Property

The HTML canvas fillStyle property defines the color, gradient, or pattern used to fill the drawing.

Syntax:

context.fillStyle=color|gradient|pattern;

value: color

Specify a green fill-color for the square:

<!DOCTYPE html>
<html>
<body>

<canvas id="square-1" width="200" height="200" style="border:1px solid #333;">
</canvas>

<script>
var c = document.getElementById("square-1");
var ctx = c.getContext("2d");
ctx.fillStyle = "#2a9d8f";
ctx.fillRect(50, 50, 100, 100);
</script>

</body>
</html>

Output:


value: gradient

Specify a gradient (top to bottom) as the fill style for the square:

<!DOCTYPE html>
<html>
<body>

<canvas id="square-2" width="200" height="200" style="border:1px solid #333;">
</canvas>

<script>
var c = document.getElementById("square-2");
var ctx = c.getContext("2d");
var create_gradient = ctx.createLinearGradient(0, 0, 0, 150);
create_gradient.addColorStop(0, "#2a9d8f");
create_gradient.addColorStop(1, "#264653");
ctx.fillStyle = create_gradient;
ctx.fillRect(50, 50, 100, 100);
</script>

</body>
</html>

Output:


Specify a gradient that goes from red, to orange, to yellow, as the fill style for the square:

<!DOCTYPE html>
<html>
<body>

<canvas id="square-3" width="200" height="200" style="border:1px solid #333;">
</canvas>

<script>
var c = document.getElementById("square-3");
var ctx = c.getContext("2d");
var createGradient = ctx.createLinearGradient(0, 0, 150, 0);
createGradient.addColorStop(0, "#e76f51");
createGradient.addColorStop(0.5, "#f4a261");
createGradient.addColorStop(1, "#e9c46a");
ctx.fillStyle = createGradient;
ctx.fillRect(50, 50, 100, 100);
</script>

</body>
</html>

Output:


To read more about HTML canvas gradients and learn how to create HTML canvas radial-gradient click here.

value: pattern

Use an image to fill the drawing:

<!DOCTYPE html>
<html>
<body>

<p>Image:</p>
<img src="https://lenadesign.org/wp-content/uploads/2021/06/car.png" id="car" width="39" height="33">
<p>Canvas:</p>
<canvas id="myPattern" width="200" height="200" style="border:1px solid #333;">
</canvas>
<br><br>

<button onclick="draw('repeat')">Repeat</button> 
<button onclick="draw('no-repeat')">No-repeat</button>     

<script>
function draw(direction) {
  var c = document.getElementById("myPattern");
  var ctx = c.getContext("2d");
  ctx.clearRect(0, 0, c.width, c.height); 
  var img = document.getElementById("car")
  var pat = ctx.createPattern(img, direction);
  ctx.rect(0, 0, 200, 200);
  ctx.fillStyle = pat;
  ctx.fill();
}
</script>

</body>
</html>

Output:

Image:

Canvas:



Enjoy coding!

Categories
Web development

HTML – Event Attributes

HTML Event Attributes

HTML is able to allow an event trigger action in a browser, like starting JavaScript when a user clicks on an element.

Events (window event attributes) triggered for the window object (in the <body> element):

onafterprint (value-script) – the event starts when a page has started printing, or if the print dialogue box has been closed.

Syntax:

<body onafterprint="script"> 

onbeforeprint (value- script) – the event starts when a page is about to be printed.

Syntax:

<body onbeforeprint="script"> 

onbeforeunload (value- script) – the event starts when the document is about to be unloaded.

Syntax:

 <body onbeforeunload="script">

onerror (value- script) – the event starts when an error occurred while loading an external file.

Syntax:

<body onerror="script"> 

onhashchange (value- script) – the event starts when there have been changes to the anchor part (begins with a ‘#’ symbol) of the current URL.

Syntax:

<element onhashchange="script"> 

Example:

<!DOCTYPE html>
<html>
<body onhashchange="onhashchangeExample()">

<p>Press the button to change the anchor part of the current URL to #newPart.</p>

<button onclick="changePart()">Try it</button>

<p id="example"></p>

<script>
// Use the location.hash property to change the anchor part
function changePart() {
  location.hash = "newPart";
  var x = "The anchor part is now: " + location.hash;
  document.getElementById("example").innerHTML = x;
}

// text alert 
function onhashchangeExample() {
  alert("The anchor part has changed!");
}
</script>

</body>
</html>

Output:

Press the button to change the anchor part of the current URL to #newPart.

onload (value- script) – the event starts when an object has been loaded.

Syntax:

<body onload="script"> 

onmessage (value- script) – the event starts when the message is triggered.

onoffline (value- script) – the event starts when the browser starts to work offline.

Syntax:

<body onoffline="script"> 

ononline (value- script) – the event starts when the browser starts to work online.

Syntax:

<body ononline="script"> 

onpagehide (value- script) – the event starts when a user navigates away from a website.

onpageshow (value- script) – the event starts when a user navigates to a website.

Syntax:

<body onpageshow="script"> 

onpopstate (value- script) – the event starts when the window’s history changes.

onresize (value- script) – the event starts when the browser window is resized.

Syntax:

<body onresize="script"> 

Example:

<!DOCTYPE html>
<html>

<body onresize="onresizeExample()">

<p>To see the alert, resize the browser window.</p>

<script>
function onresizeExample() {
  alert("You have changed the size of the browser window!");
}
</script>

</body>
</html>

Output:

To see the alert, resize the browser window.


onstorage (value- script) – the event starts when a Web Storage area is updated.

onunload (value- script) – once a page has unloaded (or the browser window has been closed).

Syntax:

<element onunload="script"> 

Events (form events) triggered by actions inside a HTML form:

onblur (value- script) – the event starts the moment that the element loses focus.

Syntax:

<element onblur="script"> 

Example:

<!DOCTYPE html>
<html>
<body>

Enter your name: <input type="text" name="name" id="name" onblur="onblurExample()">

<p>When you leave the input field, a function is triggered which transforms the input text to upper case.</p>

<script>
function onblurExample() {
  var x = document.getElementById("name");
  x.value = x.value.toUpperCase();
}
</script>

</body>
</html>

Output:

Enter your name:

When you leave the input field, a function is triggered which transforms the input text to upper case.


onchange (value- script) – the event starts the moment when the value of the element is changed.

Syntax:

<element onchange="script"> 

Example:

<!DOCTYPE html>
<html>
<body>

<p>Select an option from the list.</p>

<select id="optionList" onchange="onchangeExample()">
    <option value="HTML">HTML</option>
    <option value="CSS">CSS</option>
    <option value="JavaScript">JavaScript</option>
    <option value="jQuery">jQuery</option>
</select>

<p>When you select an option, a function is triggered which outputs the value of the selected option.</p>

<p id="example"></p>

<script>
function onchangeExample() {
  var x = document.getElementById("optionList").value;
  document.getElementById("example").innerHTML = "You selected: " + x;
}
</script>

</body>
</html>

Output:

Select an option from the list.

When you select an option, a function is triggered which outputs the value of the selected option.


oncontextmenu (value- script) – the event starts when the user right-clicks on an element to open the context menu.

Note: The contextmenu attribute only works in Firefox!

Syntax:

<element oncontextmenu="script"> 

onfocus (value- script) – the event starts the moment that the element gets focus.

Syntax:

<element onfocus="script"> 

Example:

<!DOCTYPE html>
<html>
<body>

Name: <input type="text" id="first-name" onfocus="onfocusExample(this.id)"><br>
Surname: <input type="text" id="last-name" onfocus="onfocusExample(this.id)">

<script>
function onfocusExample(x) {
  document.getElementById(x).style.background = "#2a9d8f";
}
</script>

</body>
</html>

A function is triggered when one of the input fields gets focus (the function changes the background color of the input field).

Output:

Name:
Surname:

oninput (value- script) – the event starts when an element gets user input.

Syntax:

<element oninput="script"> 

Example:

<!DOCTYPE html>
<html>
<body>

<p>Type something in the text field to trigger a function.</p>

<input type="text" id="myText" oninput="oninputExample()">

<p id="example"></p>

<script>
function oninputExample() {
  var x = document.getElementById("myText").value;
  document.getElementById("example").innerHTML = "You wrote: " + x;
}
</script>

</body>
</html>

Output:

Type something in the text field to trigger a function.


oninvalid (value- script) – the event starts when a submittable <input> element is invalid.

Syntax:

<element oninvalid="script"> 

onreset (value- script) – the event starts when a form is reset.

Syntax:

 <element onreset="script"> 

onselect (value- script) – the event starts after some text has been selected in an element.

Syntax:

<element onselect="script"> 

Example:

<!DOCTYPE html>
<html>
<body>

Select the text: <input type="text" value="Select me!" onselect="onselectExample()">

<script>
function onselectExample() {
  alert("Thanks! You have selected some text!");
}
</script>

</body>
</html>

Output:

Select the text:

onsubmit (value- script) – the event starts when a form is submitted.

Syntax:

<form onsubmit="script"> 

Example:

<!DOCTYPE html>
<html>
<body>

<form action="" onsubmit="onsubmitExample()">
  Name: <input type="text" name="firstName">
  <input type="submit" value="Submit">
</form>

<script>
function onsubmitExample() {
  alert("The form was submitted");
}
</script>

</body>
</html>

Output:

Name:

The event (keyboards events) starts when the user is pressing/ releasing a key:

onkeydown (value- script) – the event starts when a user is pressing a key.

Syntax:

<element onkeydown="script"> 

Example:

<!DOCTYPE html>
<html>
<body>

<input type="text" onkeydown="onkeydownExample()">

<script>
function onkeydownExample() {
  alert("You pressed a key inside the input field");
}
</script>

</body>
</html>

Output:


onkeypress (value- script) – the event starts when the user presses a key (on the keyboard).

Syntax:

<element onkeypress="script"> 

onkeyup (value- script) – the event starts when the user releases a key (on the keyboard).

Syntax:

<element onkeyup="script"> 

Example:

<!DOCTYPE html>
<html>
<body>

<p>Type some text (the function transforms the character to upper case):</p>
Type some text: <input type="text" id="someText" onkeyup="onkeyupExample()">

<script>
function onkeyupExample() {
  var x = document.getElementById("someText");
  x.value = x.value.toUpperCase();
}
</script>

</body>
</html>

Output:

Type some text (the function transforms the character to upper case):

Type some text:

Mouse Events:

onclick (value- script) – the event starts on a mouse click on the element.

Syntax:

<element onclick="script"> 

Example:

<!DOCTYPE html>
<html>
<body>

<button onclick="onclickExample()">Click me</button>

<p id="example"></p>


<script>
function onclickExample() {
  document.getElementById("example").innerHTML = "Thanks!";
}
</script>

</body>
</html>

Output:


ondblclick (value- script) – the event starts on a mouse double-click on the element.

Syntax:

<element ondblclick="script"> 

Example:

<!DOCTYPE html>
<html>
<body>

<p id="example" ondblclick="ondblclickExample()">Double-click on the text to change the text color.</p>

<script>
function ondblclickExample() {
  document.getElementById("example").style.color = "#2a9d8f";
}
</script>

</body>
</html>

Output:

Double-click on the text to change the text color.


onmousedown (value- script) – the event starts when a mouse button is pressed down on the element.

Syntax:

<element onmousedown="script"> 

onmousemove (value- script) – the event starts when the pointer is moving while it is over an element.

Syntax:

<element onmousemove="script"> 

Example:

<!DOCTYPE html>
<html>
<body>

<img onmousemove="largeImage(this)" onmouseout="image(this)" border="0" src="https://lenadesign.org/wp-content/uploads/2021/06/icon.png" alt="car" width="58" height="49">

<script>
function largeImage(x) {
  x.style.height = "69px";
  x.style.width = "78px";
}

function image(x) {
  x.style.height = "49px";
  x.style.width = "58px";
}
</script>

</body>
</html>

Output:

car

onmouseout (value- script) – the event starts when the mouse pointer moves out of an element.

Syntax:

<element onmouseout="script"> 

onmouseover (value- script) – the event starts when the mouse pointer moves over an element.

Syntax:

<element onmouseover="script"> 

onmouseup (value- script) – the event starts when a mouse button is released over the element.

Syntax:

<element onmouseup="script"> 

Example:

<!DOCTYPE html>
<html>
<body>

<p id="p-1" onmousedown="mouseDown()" onmouseup="mouseUp()">
Click the text! The mouseDown() function is triggered when the mouse button is pressed down over this paragraph. The function sets the color of the text to red. The mouseUp() function is triggered when the mouse button is released. The mouseUp() function sets the color of the text to green.
</p>

<script>
function mouseDown() {
  document.getElementById("p-1").style.color = "#e76f51";
}

function mouseUp() {
  document.getElementById("p-1").style.color = "#2a9d8f";
}
</script>

</body>
</html>

Output:

Click the text! The mouseDown() function is triggered when the mouse button is pressed down over this paragraph. The function sets the color of the text to red. The mouseUp() function is triggered when the mouse button is released. The mouseUp() function sets the color of the text to green.


onwheel (value- script) – the event starts when the wheel of a pointing device is rolled up or down over an element.

Syntax:

<element onwheel="script"> 

Example:

<!DOCTYPE html>
<html>
<head>
<style>
#example {
  border: 2px solid #333;
}
</style>
</head>
<body>

<div id="example" onwheel="onwheelExample()">This example demonstrates how to assign an "onwheel" event event to a DIV element. Roll the mouse wheel over me - either up or down!</div>

<script>
function onwheelExample() {
  document.getElementById("example").style.fontSize = "30px";
}
</script>

</body>
</html>

Output:

This example demonstrates how to assign an “onwheel” event event to a DIV element. Roll the mouse wheel over me – either up or down!

Drag Events:

ondrag (value- script) – the event starts when an element or text selection is being dragged.

Syntax:

<element ondrag="script"> 

Example:

<!DOCTYPE HTML>
<html>
<head>
<style>
.drop-target {
  float: left; 
  width: 100px; 
  height: 50px;
  text-align: center;
  margin:20px;
  border: 2px solid #333;
}
</style>
</head>
<body>

<div class="drop-target" ondrop="drop(event)" ondragover="allowDrop(event)">
  <p ondragstart="dragStart(event)" ondrag="dragging(event)" draggable="true" id="drag-target">Drag me!</p>
</div>

<div class="drop-target" ondrop="drop(event)" ondragover="allowDrop(event)"></div>

<p style="clear:both;" id="exampleOne"></p>

<script>
function dragStart(event) {
  event.dataTransfer.setData("Text", event.target.id);
}

function dragging(event) {
  document.getElementById("exampleOne").innerHTML = "The p element is being dragged";
}

function allowDrop(event) {
  event.preventDefault();
}

function drop(event) {
  event.preventDefault();
  var data = event.dataTransfer.getData("Text");
  event.target.appendChild(document.getElementById(data));
  document.getElementById("exampleOne").innerHTML = "The p element was dropped";
}
</script>

</body>

Output:

Drag me!


ondragend (value- script) – the event starts when the user has finished dragging an element or text selection.

Syntax:

<element ondragend="script"> 

ondragenter (value- script) – the event starts when a draggable element or text selection enters a valid drop target.

Syntax:

<element ondragenter="script"> 

Example:

<!DOCTYPE HTML>
<html>
<head>
<style>
.droptarget {
  float: left; 
  width: 100px; 
  height: 50px;
  margin: 20px;
  text-align: center;
  border: 2px solid #333;
}
</style>
</head>
<body>

<div class="droptarget" ondrop="drop(event)" ondragenter="dragEnter(event)" ondragleave="dragLeave(event)" ondragover="allowDrop(event)">
  <p ondragstart="dragStart(event)" draggable="true" id="dragtarget">Drag me!</p>
</div>

<div class="droptarget" ondragenter="dragEnter(event)" ondragleave="dragLeave(event)" ondrop="drop(event)" ondragover="allowDrop(event)"></div>

<p style="clear:both;" id="exampleTwo"></p>

<script>
function dragStart(event) {
  event.dataTransfer.setData("Text", event.target.id);
}

function dragEnter(event) {
  if ( event.target.className == "droptarget" ) {
    event.target.style.border = "5px dashed #264653";
    document.getElementById("exampleTwo").innerHTML = "Entered the dropzone";
  }
}

function dragLeave(event) {
  if ( event.target.className == "droptarget" ) {
    event.target.style.border = "";
    document.getElementById("exampleTwo").innerHTML = "Left the dropzone";
  }
}

function allowDrop(event) {
  event.preventDefault();
}

function drop(event) {
  event.preventDefault();
  var data = event.dataTransfer.getData("Text");
  event.target.appendChild(document.getElementById(data));
}
</script>

</body>
</html>

Output:

Drag me!

ondragleave (value- script) – the event starts when a draggable element or text selection leaves a valid drop target.

Syntax:

<element ondragleave="script"> 

ondragover (value- script) – the event starts when a draggable element or text selection is being dragged over a valid drop target.

Syntax:

<element ondragover="script"> 

Example:

<!DOCTYPE HTML>
<html>
<head>
<style>
#dropTarget {
  float: left; 
  width: 300px; 
  height: 100px;
  margin: 20px;
  text-align: center;
  border: 2px solid #333;
}
</style>
</head>
<body>

<p ondragstart="dragStart(event)" draggable="true" id="dragtarget">Drag the text into the rectangle!</p>

<div id="dropTarget" ondrop="drop(event)" ondragover="allowDrop(event)">
</div>

<p style="clear:both;" id="exampleThree"></p>

<script>
function dragStart(event) {
  event.dataTransfer.setData("Text", event.target.id);
}

function allowDrop(event) {
  event.preventDefault();
  document.getElementById("exampleThree").innerHTML = "The p element is OVER the droptarget.";
  event.target.style.border = "5px dashed #264653";
}

function drop(event) {
  event.preventDefault();
  var data = event.dataTransfer.getData("Text");
  event.target.appendChild(document.getElementById(data));
  document.getElementById("exampleThree").innerHTML = "The p element was dropped.";
}
</script>

</body>
</html>

Output:

Drag the text into the rectangle!


ondragstart (value- script) – the event starts when the user starts to drag an element or text selection.

Syntax:

<element ondragstart="script"> 

ondrop (value- script) – the event starts when a draggable element or text selection is dropped on a valid drop target.

Syntax:

<element ondrop="script"> 

onscroll (value- script) – the event starts when an element’s scrollbar is being scrolled.

Syntax:

<element onscroll="script"> 

Example:

<!DOCTYPE html>
<html>
<head>
<style>
#text {
  border: 2px solid #333;
  width: 350px;
  height: 150px;
  overflow: scroll;
}
</style>
</head>
<body>

<div id="text" onscroll="onscrollExample()">He  was  an  old  man  who  fished  alone  in  a  skiff  in  the  Gulf  Stream  and  he  had  gone  eighty-four days now without taking a fish. In the first forty days a boy had been with him. But  after  forty  days  without  a  fish  the  boy’s  parents  had  told  him  that  the  old  man  was  now definitely and finally salao, which is the worst form of unlucky, and the boy had gone at  their  orders  in  another  boat  which  caught  three  good  fish  the  first  week. 
<br><br>
It  made  the  boy  sad  to  see  the  old  man  come  in  each  day  with  his  skiff  empty  and  he  always  went  down to help him carry either the coiled lines  or  the  gaff  and  harpoon  and  the  sail  that  was  furled  around  the  mast.  The  sail  was  patched  with  flour  sacks and, furled, it looked like the flag of permanent defeat. </div>

<script>
function onscrollExample() {
  document.getElementById("text").style.color = "#2a9d8f";
}
</script>

</body>
</html>

Output:

He was an old man who fished alone in a skiff in the Gulf Stream and he had gone eighty-four days now without taking a fish. In the first forty days a boy had been with him. But after forty days without a fish the boy’s parents had told him that the old man was now definitely and finally salao, which is the worst form of unlucky, and the boy had gone at their orders in another boat which caught three good fish the first week.

It made the boy sad to see the old man come in each day with his skiff empty and he always went down to help him carry either the coiled lines or the gaff and harpoon and the sail that was furled around the mast. The sail was patched with flour sacks and, furled, it looked like the flag of permanent defeat.

Clipboard Events:

oncopy (value- script) – the event starts when the user copies the content of an element.

Syntax:

<element oncopy="script"> 

Example:

<!DOCTYPE html>
<html>
<body>

<input type="text" oncopy="oncopyExample()" value="Try to copy this text">

<p id="example"></p>

<script>
function oncopyExample() {
  document.getElementById("example").innerHTML = "You copied text!"
}
</script>

</body>
</html>

Output:


oncut (value- script) – the event starts when the user cuts the content of an element.

Syntax:

<element oncut="script"> 

onpaste (value- script) – the event starts when the user pastes some content in an element.

Syntax:

<element onpaste="script"> 

Enjoy coding!

Categories
Web development

HTML map and area tags

HTML map and area tags

The HTML <map> element is used to specify an image map. An image map is an image with clickable areas.

The HTML <map> tag contains a number of HTML <area> tags, that specify the clickable areas in the image map.

Example:

<!DOCTYPE html>
<html>
<body>

<p>Click on the laptop, or the cup of coffee:</p>

<img src="https://lenadesign.org/wp-content/uploads/2021/06/homeOffice.jpg" alt="homeOffice" usemap="#workmap" width="640" height="480">

<map name="workmap">
  <area shape="rect" coords="34,44,270,350" alt="laptop" href="https://lenadesign.org/wp-content/uploads/2021/06/laptop.jpg">
  <area shape="circle" coords="337,300,44" alt="coffee" href="https://lenadesign.org/wp-content/uploads/2021/06/photoshop-gif.gif">
</map>

</body>
</html>

Output:

Click on the laptop, or the cup of coffee:

homeOffice laptop coffee

Note: The usemap attribute in <img> is linked with the <map> element’s name attribute, and creates a connection between the image and the map.

The HTML <area> element specifies an area inside an image map.

Attributes:

alt (value- text) -defines an alternate text for the area.

coords (value- coordinates) – defines the coordinates of the area.

x1,y1,x2,y2 – defines the coordinates of the top-left and bottom-right corner (shape=”rect”).

x,y,radius – defines the coordinates of the circle center and the radius (shape=”circle”).

x1,y1,x2,y2,..,xn,yn – defines the coordinates of the edges of the polygon.

download (value- filename) – defines that the target will be downloaded when a user clicks on the hyperlink.

href (value- URL) – defines the hyperlink target for the area.

media (value- media query) – defines what device the target URL is optimized for.

referrerpolicy (value- no-referrer/ no-referrer-when-downgrade/ origin/ origin-when-cross-origin/ same-origin/ strict-origin-when-cross-origin/ unsafe-url) – defines which referrer information to send with the link.

rel (value- alternate/ author/ bookmark/ help/ license/ next/ nofollow/ noreferrer/ prefetch/ prev/ search/ tag) – defines the relationship between the current document and the target URL.

shape (value- default/ rect/ circle/ poly) – defines the shape of the area.

target (value- _blank/ _parent/ _self/ _top/ framename) – defines where to open the target URL.

type (value- media_type) – defines the media type of the target URL.

Enjoy coding!

Read also:

HTML meter tag

HTML fieldset tag & legend tag

HTML input tag

Categories
Web development

The basic structure of an HTML document: doctype declaration, html tag, head tag, and body tag

The basic structure of an HTML document

The basic structure of an HTML document contains 5 elements:

  1. <!DOCTYPE>
  2. <html>
  3. <head>
  4. <title>
  5. <body>

HTML <!DOCTYPE>

Every HTML document must start with a <!DOCTYPE> declaration. The <!DOCTYPE> declaration is not an HTML tag. It is “information” to the browser about what document type to expect.

In HTML5, use the declaration:

 <!DOCTYPE html>

Example:

<!DOCTYPE html>
<html>
<head>
<title>This is a title</title>
</head>

<body>
This is the content of the document.
</body>

</html>

Output:

This is a title
This is the content of the document.

HTML <html> tag

The HTML <html> element expresses the root of an HTML document.

The HTML <html> element is the container for all other HTML elements.

Example:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>This is a title</title>
</head>
<body>

<h4>This is a heading.</h4>
<p>This is a paragraph.</p>

</body>
</html>

Output:

Tip: Include the lang attribute inside the element, to declare the language of the website (this is meant to assist search engines and browsers.):

This is a title

This is a heading.

This is a paragraph.


HTML <head> tag

The HTML <head> tag is a container for metadata (information about data) and is placed between the <html> tag and the <body> tag.

The metadata typically specifies the document title, character set, styles, scripts, and other meta information.

Inside the HTML <head> tag can be the following tags:

<base>

<!DOCTYPE html>
<html>
<head>
  <base href="https://lenadesign.org/" target="_blank">
</head>
<body>

<h4>The HTML base element.</h4>

<p><a href="https://lenadesign.org/">HTML base tag</a> - Notice that the link opens in a new window, even if it has no target="_blank" attribute. This is because the target attribute of the base element is set to "_blank".</p>

</body>
</html>

Output:

The HTML base element.

HTML base tag – Notice that the link opens in a new window, even if it has no target=”_blank” attribute. This is because the target attribute of the base element is set to “_blank”.


<link>

<meta>

<noscript>

<title> (required!)

<script>

<style>

HTML <body> tag

The HTML <body> element specifies the document’s body.

The HTML <body> tag may contain all the contents of an HTML document, such as headings, paragraphs, images, hyperlinks, tables, etc.

Example:

<!DOCTYPE html>
<html>
<head>
  <title>This is a title</title>
</head>

<body>
  <h4>This is a heading.</h4>
  <p>This is a paragraph.</p>
</body>

</html>

Output:

This is a title

This is a heading.

This is a paragraph.


Enjoy coding!

Categories
Web development

HTML style tag

HTML style tag

The HTML <style> element defines style information (CSS) for a document.

Inside the <style> tag you can define how HTML elements should render in a browser.

Example:

<!DOCTYPE html>
<html>
<head>
<style>
h4 {color:#2a9d8f;}
p {color:#e76f51;}
</style>
</head>
<body>

<h4>This is a heading</h4>
<p>This is a paragraph.</p>

</body>
</html>

Output:

This is a heading

This is a paragraph.

Note: To connect to an external style sheet, use the HTML <link> element.

Attributes:

media (value- media_query) – defines what media/device the media resource is optimized for.

Syntax:

<style media="value"> 

type (value- text/css) – defines the media type of the <style> element.

Syntax:

<style type="media_type"> 

To learn more about HTML <style> element go to the CSS tutorials.

Enjoy coding!

Categories
Web development

HTML link tag

HTML link tag

The HTML <link> element specifies the connection between the current document and an external resource.

The HTML <link> element is often used to link to external style sheets (CSS).

The HTML <link> tag is an empty element (it contains only attributes).

The HTML <link> element should be placed in the <head> section of the document.

Example:

<!DOCTYPE html>
<html>
<head>
  <link rel="stylesheet" href="example-style.css">
</head>
<body>

<h4>This is a heading.</h4>

<p>This is a paragraph.</p>

<p>This is the second paragraph.</p>

</body>
</html>

Output:

HTML link tag

Attributes:

crossorigin (value- anonymous/ use-credentials) – defines how the tag handles cross-origin requests.

href (value- URL) – defines the location of the linked file.

hreflang (value- language_code) – defines the language of the text in the linked file.

media (value- media_query) – defines what kind of device the linked file will be displayed.

referrer policy (value- no-referrer/ no-referrer-when-downgrade/ origin/ origin-when-cross-origin/ unsafe-url) – defines which referrer to use when fetching the resource.

rel (required), (value- alternate/ author/ dns-prefetch/ help/ icon/ license/ next/ pingback/ preconnect/ prefetch/ preload/ prerender/ prev/ search/ stylesheet) – defines the connection between the current document and the linked file.

size (value- HeightxWidth/ any) – defines the size of the linked resource. Only for rel=”icon“.

type (value- media_type) – defines the media type of the linked file.

Enjoy coding!

Read also:

HTML style tag

HTML progress tag