Table of Contents
Core Concept
CSS Structure
JavaScript Implementation
Frequently Asked Questions
What is the cc() function?
Why use clip-path if the mask already clips?
Why use @property with the border value?
What about the --path variable?
Can we have a dashed border?
Why not use @property for the dash variable?
Practical Applications
Home Web Front-end CSS Tutorial Exploring the CSS Paint API: Polygon Border

Exploring the CSS Paint API: Polygon Border

Mar 20, 2025 am 09:47 AM

Exploring the CSS Paint API: Polygon Border

Creating intricate shapes with clip-path is straightforward, but adding borders presents a persistent challenge. CSS lacks a robust solution, often requiring cumbersome workarounds. This article demonstrates a solution using the CSS Paint API.

This CSS Paint API exploration series continues:

  • Part 1: Image Fragmentation Effect
  • Part 2: Blob Animation
  • Part 3: Polygon Border (current article)
  • Part 4: Rounding Shapes

This article details creating polygon borders. Remember, this technique is currently supported only in Chromium-based browsers (Chrome, Edge, Opera). Check caniuse for the latest compatibility information.

The code remains concise and adaptable, requiring only minor variable adjustments to modify the shape.

Core Concept

The polygon border is achieved by combining clip-path and a custom mask generated with the Paint API:

  1. Begin with a standard rectangle.
  2. Apply clip-path to shape it into a polygon.
  3. Apply a custom mask to create the polygon border.

CSS Structure

The CSS for the clip-path step is:

.box {
  --path: 50% 0,100% 100%,0 100%;

  width: 200px;
  height: 200px;
  background: red;
  display: inline-block;
  clip-path: polygon(var(--path));
}
Copy after login

The key is the CSS variable --path. Both clip-path and the mask utilize this variable for consistent parameters.

The complete CSS code becomes:

.box {
  --path: 50% 0,100% 100%,0 100%;
  --border: 5px;

  width: 200px;
  height: 200px;
  background: red;
  display: inline-block;
  clip-path: polygon(var(--path));
  -webkit-mask: paint(polygon-border);
}
Copy after login

Besides clip-path, a custom mask is applied, and --border controls border thickness. The CSS remains simple and generic, highlighting the Paint API's ease of use.

JavaScript Implementation

Refer to Part 1 of this series for a better understanding of the Paint API structure.

The paint() function's JavaScript code:

const points = properties.get('--path').toString().split(',');
const b = parseFloat(properties.get('--border').value);
const w = size.width;
const h = size.height;

const cc = function(x,y) {
  // ...
}

var p = points[0].trim().split(" ");
p = cc(p[0],p[1]);

ctx.beginPath();
ctx.moveTo(p[0],p[1]);
for (var i = 1; i 
<p>The code reads the <code>--path</code> variable, converts it into a point array, and then draws a polygon using <code>moveTo</code> and <code>lineTo</code>. This polygon mirrors the <code>clip-path</code> polygon.  The stroke creates the border; the shape's fill is transparent.</p>
<p>Modifying the path and thickness allows for diverse polygon borders.  Gradients and images can replace solid colors due to the use of the CSS <code>background</code> property.</p>
<p>To incorporate content, a pseudo-element is necessary to prevent clipping.  The mask property is moved to the pseudo-element, while <code>clip-path</code> remains on the main element.</p>
<h3 id="Frequently-Asked-Questions">Frequently Asked Questions</h3>
<p>Several common questions regarding the provided script are addressed below.</p>
<h4 id="What-is-the-code-cc-code-function">What is the <code>cc()</code> function?</h4>
<p>This function converts point coordinates (percentage or pixel values) into pixel values usable within the canvas element.</p>
<pre class="brush:php;toolbar:false">const cc = function(x,y) {
  var fx=0,fy=0;
  if (x.indexOf('%') > -1) {
    fx = (parseFloat(x)/100)*w;
  } else if(x.indexOf('px') > -1) {
    fx = parseFloat(x);
  }
  if (y.indexOf('%') > -1) {
     fy = (parseFloat(y)/100)*h;
  } else if(y.indexOf('px') > -1) {
    fy = parseFloat(y);
  }
  return [fx,fy];
}
Copy after login

Why use clip-path if the mask already clips?

Using only the mask leads to issues with stroke alignment and hoverable area. clip-path resolves these problems.

Why use @property with the border value?

@property registers the custom property, defining its type (in this case, <length></length>), enabling browser recognition and handling as a valid type, not just a string. This allows for various length units.

What about the --path variable?

The --path variable is handled as a string due to limitations in registering complex types with @property. The cc() function handles the string-to-pixel conversion.

Can we have a dashed border?

Yes, using ctx.setLineDash(). An additional variable controls the dash pattern.

Why not use @property for the dash variable?

While technically possible, retrieving the values within paint() proved problematic. For now, the --dash variable is treated as a string.

Practical Applications

Several use cases showcase the polygon border technique:

  • Buttons: Create custom-shaped buttons with hover effects.
  • Breadcrumbs: Simplify breadcrumb navigation.
  • Card Reveal Animation: Animate border thickness for hover effects or reveal animations.
  • Callouts & Speech Bubbles: Easily add borders to complex shapes.
  • Animating Dashes: Create various dash animations using setLineDash() and lineDashOffset.

This article provides a comprehensive guide to creating polygon borders using the CSS Paint API, offering flexibility and efficiency in shape styling.

The above is the detailed content of Exploring the CSS Paint API: Polygon Border. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

Google Fonts   Variable Fonts Google Fonts Variable Fonts Apr 09, 2025 am 10:42 AM

I see Google Fonts rolled out a new design (Tweet). Compared to the last big redesign, this feels much more iterative. I can barely tell the difference

How to Create an Animated Countdown Timer With HTML, CSS and JavaScript How to Create an Animated Countdown Timer With HTML, CSS and JavaScript Apr 11, 2025 am 11:29 AM

Have you ever needed a countdown timer on a project? For something like that, it might be natural to reach for a plugin, but it’s actually a lot more

Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Apr 05, 2025 pm 05:51 PM

Questions about purple slash areas in Flex layouts When using Flex layouts, you may encounter some confusing phenomena, such as in the developer tools (d...

How to select a child element with the first class name item through CSS? How to select a child element with the first class name item through CSS? Apr 05, 2025 pm 11:24 PM

When the number of elements is not fixed, how to select the first child element of the specified class name through CSS. When processing HTML structure, you often encounter different elements...

HTML Data Attributes Guide HTML Data Attributes Guide Apr 11, 2025 am 11:50 AM

Everything you ever wanted to know about data attributes in HTML, CSS, and JavaScript.

A Proof of Concept for Making Sass Faster A Proof of Concept for Making Sass Faster Apr 16, 2025 am 10:38 AM

At the start of a new project, Sass compilation happens in the blink of an eye. This feels great, especially when it’s paired with Browsersync, which reloads

See all articles