HTML Canvas shadowOffsetY Property: Setting Shadow Offset
The HTML Canvas shadowOffsetY property is a key tool for controlling the vertical offset of shadows in your canvas drawings. This property allows you to move shadows up or down from the drawn shape, adding depth and visual interest to your graphics. Understanding and using this property is essential for creating effective and visually appealing effects on your canvas.
What is the shadowOffsetY Property?
The shadowOffsetY property specifies the vertical distance the shadow is offset from the shape. A positive value moves the shadow downwards, while a negative value moves the shadow upwards. The value is measured in pixels.
Purpose of the shadowOffsetY Property
The primary purpose of the shadowOffsetY property is to:
- Control the vertical position of a shadow.
- Create the illusion of a light source being above or below an object.
- Enhance the depth and dimensionality of your canvas drawings.
- Produce varied shadow effects by adjusting the offset.
Syntax of the shadowOffsetY Property
The shadowOffsetY property is set directly on the 2D rendering context of the canvas. Here's the syntax:
ctx.shadowOffsetY = value;
ctx: The 2D rendering context of the canvas.value: A number representing the vertical offset of the shadow in pixels. Positive values move the shadow down, negative values move the shadow up.
Examples of Using shadowOffsetY
Let's explore some examples to demonstrate how the shadowOffsetY property works in practice. Each example will include the HTML and JavaScript code along with the rendered output.
Basic Shadow Offset
In this example, we draw a rectangle with a shadow that is offset 10 pixels vertically downwards.
<canvas
id="canvasShadowOffsetYBasic"
width="200"
height="150"
style="border: 1px solid black;"
></canvas>
<script></script>
Shadow Offset Upwards
Here, we draw a circle with a shadow that is offset 10 pixels vertically upwards.
<canvas
id="canvasShadowOffsetYUp"
width="200"
height="150"
style="border: 1px solid black;"
></canvas>
<script>
//<![CDATA[
const canvas_up = document.getElementById("canvasShadowOffsetYUp");
const ctx_up = canvas_up.getContext("2d");
ctx_up.shadowColor = "gray";
ctx_up.shadowBlur = 5;
ctx_up.shadowOffsetX = 0;
ctx_up.shadowOffsetY = -10;
ctx_up.beginPath();
ctx_up.arc(100, 75, 50, 0, 2 * Math.PI);
ctx_up.fillStyle = "lightgreen";
ctx_up.fill();
//]]]]><![CDATA[>
</script>
Varying Shadow Offset
This example shows different shadows with different shadowOffsetY values, demonstrating the versatility of this property.
<canvas
id="canvasShadowOffsetYVary"
width="400"
height="200"
style="border: 1px solid black;"
></canvas>
<script>
//<![CDATA[
const canvas_vary = document.getElementById("canvasShadowOffsetYVary");
const ctx_vary = canvas_vary.getContext("2d");
const shapes = [
{ x: 50, y: 50, offset: -20, color: "lightcoral" },
{ x: 150, y: 50, offset: 0, color: "lightblue" },
{ x: 250, y: 50, offset: 20, color: "lightgoldenrodyellow" },
{ x: 350, y: 50, offset: 40, color: "lightseagreen" },
];
shapes.forEach((shape) => {
ctx_vary.shadowColor = "gray";
ctx_vary.shadowBlur = 5;
ctx_vary.shadowOffsetX = 0;
ctx_vary.shadowOffsetY = shape.offset;
ctx_vary.fillStyle = shape.color;
ctx_vary.fillRect(shape.x, shape.y, 50, 50);
});
//]]]]><







