• Stars
    star
    230
  • Rank 174,053 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 3 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A tiny CSS Houdini module that allows to add a squircle shape to HTML elements

Squircle CSS Houdini

Cover

A tiny CSS Houdini module that allows adding a squircle shape to HTML elements.


🎻  Demo on GitHub Pages

πŸ‘ΎΒ Β Codepen examples

πŸ“¦Β Β NPM package

πŸ—žΒ Β Medium article


πŸš€ Usage

Add the script

In order to use the module, you need to add the script to your HTML file. But it's not a usual JS module that you can import.

// Vanilla JS and Create React App
// Add the script to the index.html file

<script>
  if ("paintWorklet" in CSS) {
    CSS.paintWorklet.addModule(
      "https://www.unpkg.com/css-houdini-squircle/squircle.min.js"
    );
  }
</script>
// NextJS for TSX files
// Add the script to the _app.js file (or any other file that is loaded on every page)

React.useEffect(() => {
  (CSS as any).paintWorklet.addModule("squircle.min.js");
}, []);

Add the styles

/* use mask */
.squircle {
  width: 200px;
  height: 200px;
  background: linear-gradient(45deg, yellow, blue);
  --squircle-smooth: 1;
  --squircle-radius: 10px;
  mask-image: paint(squircle);
}
/* use background */
.squircle {
  width: 200px;
  height: 200px;
  background: paint(squircle);
  --squircle-smooth: 1;
  --squircle-radius: 10px;
  --squircle-fill: #f45;
}

πŸ‘‰ codepen example


πŸŽ›Β Β Custom CSS Properties

--squircle-radius

--squircle-radius property

The property controls the roundness of the corners. You can provide 1, 2, 3 or 4 values, similar to padding/margin in CSS. The order is clockwise: top left, top right, bottom right, bottom left

  • Syntax: <px> OR <px px> OR <px px px> OR <px px px px>
  • Defaul value: 8px (if no radius at all is defined) OR: 0 (if only some radii are defined)
  • Min/Max values: β€” (the radii are capped at half of the shorter side of width/ height)

--squircle-radius-top-left, --squircle-radius-top-right, --squircle-radius-bottom-right, --squircle-radius-bottom-left

Set radii for the corners individually

/* Usage */

.squircle {
  /* other properties */
  width: 200px;
  height: 200px;
  background: paint(squircle);
  /* the property */
  --squircle-radius: 20px;
}

individual border radius

individual border radius

The property controls the roundness of the corners individually.

/* Usage */

.squircle {
  /* other properties */
  width: 200px;
  height: 200px;
  background: paint(squircle);
  /* the property */
  --squircle-radius-top-left: 0px;
  --squircle-radius-top-right: 15px;
  --squircle-radius-bottom-right: 30px;
  --squircle-radius-bottom-left: 40px;
}

--squircle-smooth

--squircle-smooth property

The property controls the length of bezier guide lines. Could be defined by --squircle-ratio.

  • Syntax: <number>
  • Defaul value: 1
  • Min/Max values: 0.1 / 1
/* Usage */

.squircle {
  /* other properties */
  width: 200px;
  height: 200px;
  background: paint(squircle);
  --squircle-radius: 20px;
  /* the property */
  --squircle-smooth: 0.5;
}

--squircle-outline

--squircle-outline property

The property controls squircle outline. There are two methods how too use it with background-mask and mask+:pseudo-element. to find out more check codepen examples.

  • Syntax: <px>
  • Defaul value: β€”
  • Min/Max values: β€”
/* Usage */

.squircle {
  /* other properties */
  width: 200px;
  height: 200px;
  background: paint(squircle);
  --squircle-radius: 20px;
  /* the property */
  --squircle-outline: 5px;
}

--squircle-fill

--squircle-fill property

The property accepts any color, including variables.

⚠️ Work only with background: paint(squircle);. For mask-image: paint(squircle); use background property.

  • Syntax: <color>
  • Defaul value: #f45
  • Min/Max values: β€”
/* Usage */

.squircle {
  /* other properties */
  width: 200px;
  height: 200px;
  background: paint(squircle);
  --squircle-radius: 20px;
  /* the property */
  --squircle-fill: #f45;
}

πŸ•ΉΒ Β How to install particular version

// latest version
<script>
  if ("paintWorklet" in CSS) {
    CSS.paintWorklet.addModule(
      "https://www.unpkg.com/css-houdini-squircle/squircle.min.js"
    );
  }
</script>

// or particular version
<script>
  if ("paintWorklet" in CSS) {
    CSS.paintWorklet.addModule(
      "https://www.unpkg.com/[email protected]/squircle.min.js"
    );
  }
</script>

Install via NPM

npm i css-houdini-squircle

Download

You can download the min version of the module from UNPKG

// latest version
https://www.unpkg.com/browse/css-houdini-squircle/squircle.min.js

✨  Use css-paint-polyfill

In order to get the module work on other browsers, you can use Paint Worklets polyfill.

⚠️ Check for artefacts before deploying.

// use with polifill example
<script>
  (async function () {
    if (!("paintWorklet" in CSS)) {
      await import("css-paint-polyfill");
    }

    CSS.paintWorklet.addModule(
      `https://www.unpkg.com/css-houdini-squircle/squircle.min.js`
    );
  })();
</script>

Contributing and testing

If you have any ideas, just open an issue and tell what you think.

If you'd like to contribute, please fork the repository. Pull requests are warmly welcome.

The project structure is separated into nextJS app and lib folder. The lib folder contains the script itself. The nextJS app is used for the demo. The lib folder is a separate NPM package.

πŸ“ root
  πŸ“ lib
    - package.json
    - squircle.js
  πŸ“ … other nextJS folders

In order to test the script locally:

  1. you need to run npm run dev in the root folder. It will start the NextJS app.
  2. Then you need to run npm run watch:build in the lib folder. It will start the watcher for the script. It will build the script every time you change it and create squircle.min.js file in the lib folder and in the public folder of the NextJS app.
  3. In the index.tsx file of the NextJS app, you can uncomment the line with test section un comment other in order to ease the development process.

Change log (v0.3.0)

  • Removed --squircle-ratio property. It's now fixed to 1.8. It's still possible to change the ratio by changing --squircle-smooth property.
  • Moved the demo to NextJS
  • Added --squircle-radius-top-left, --squircle-radius-top-right, --squircle-radius-bottom-right, --squircle-radius-bottom-left properties
  • Added separate lib folder only for the script

More Repositories

1

warp-svg

Warp and distort SVG files online
JavaScript
188
star
2

JSON-to-Figma

An easy way to populate Figma layers with JSON
TypeScript
124
star
3

web-dark-ages

HTML
105
star
4

JSON-from-Figma

A simple example of using Figma API to get a JSON file, filter or download it
JavaScript
72
star
5

context-cursor

A cursor that takes the shape of the hovered element.
HTML
60
star
6

wunderzin

CSS
55
star
7

tiny-flexbox-grid-system

Tiny SCSS Flexbox Grid System
CSS
46
star
8

Lazy-Exportable---sketch-plugin

Makes layers exportable by types, flag or name
JavaScript
20
star
9

MaterialMotion-for-Framer

material motion fo Framer
JavaScript
19
star
10

GlitchContainer-for-FramerJS

A simple way to add glitch effect to your prototypes
CoffeeScript
18
star
11

Figma-Warp

A simple plugin that allows you to warp πŸŒ€, bend, and distort vector shapes in Figma.
TypeScript
17
star
12

figma-image-editor

Figma plugin to manipulate images color and add filters
JavaScript
14
star
13

kinetik-typography-three-js

Kinetik typography experiments with ThreeJS
JavaScript
14
star
14

Figma-Auto-Layout-Styles

The plugin brings an experience of Figma styles to the auto-layout feature. You can make separate configurations of auto-layouts, save and load them, lock or change at any time.
TypeScript
9
star
15

tinify-Image-compressor-figma

TypeScript
9
star
16

markdown-theme-shell

Parse a markdown file in the HTML page
CSS
8
star
17

FramerAR

Framer prototype with A-Frame and AR.js
HTML
5
star
18

figma-plugin-react-boilerplate

This repo offers a TypeScript-based React boilerplate for Figma plugins. It includes features such as SASS, CSS/SASS/SCSS modules, SVG image import, and up-to-date packages.
TypeScript
5
star
19

brutalist-buildings

list of brutalist buildings
CSS
4
star
20

gooey-for-framer

Gooey effect for framer. Work well in chrome broweser
JavaScript
4
star
21

textGradient-for-Framer.js

gradient for TextLayer
JavaScript
3
star
22

variants-randomizer

TypeScript
3
star
23

GPTify-Figma

TypeScript
3
star
24

berlin-brutalism

TypeScript
3
star
25

figma-stat

JavaScript
2
star
26

SVG-to-Image

TypeScript
2
star
27

Fasta-Fasta-Sketch-plugin

background timer for sketch
2
star
28

Nicolas-Cage-Figma

Nicolas Cage Replacer for Figma
TypeScript
2
star
29

revoult-budgeting-materials

2
star
30

TelegramBot

Send messages from Sketch with Telegram bot
2
star
31

JUST_DETACH

JavaScript
1
star
32

FrameLoader

Preloaders for Framer.JS
JavaScript
1
star
33

Yandex_search

test task
CSS
1
star
34

gulp-template

simple gulp template for jade, stylus and coffee
JavaScript
1
star
35

get-figma-stat

Get the stats about any Figma plugin, file or widget
TypeScript
1
star
36

pavellaptev.github.io

TypeScript
1
star
37

Framer-iOS11-components

This repository includes modules of iOS11 components.
CoffeeScript
1
star
38

figma-halftone

Figma plugin to generate halftone gradients.
TypeScript
1
star