惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

K
Kaspersky official blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
V
Visual Studio Blog
F
Full Disclosure
B
Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
L
Lohrmann on Cybersecurity
月光博客
月光博客
I
Intezer
博客园 - 三生石上(FineUI控件)
Hacker News - Newest:
Hacker News - Newest: "LLM"
D
Darknet – Hacking Tools, Hacker News & Cyber Security
博客园_首页
P
Proofpoint News Feed
C
Check Point Blog
N
News | PayPal Newsroom
H
Heimdal Security Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
G
GRAHAM CLULEY
WordPress大学
WordPress大学
C
CERT Recently Published Vulnerability Notes
Y
Y Combinator Blog
Recorded Future
Recorded Future
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
Tailwind CSS Blog
W
WeLiveSecurity
L
LINUX DO - 热门话题
Microsoft Azure Blog
Microsoft Azure Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Schneier on Security
Schneier on Security
爱范儿
爱范儿
Martin Fowler
Martin Fowler
U
Unit 42
T
Troy Hunt's Blog
S
Securelist
V
V2EX
V2EX - 技术
V2EX - 技术
MongoDB | Blog
MongoDB | Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - 聂微东
人人都是产品经理
人人都是产品经理
M
MIT News - Artificial intelligence
T
Tor Project blog
Cisco Talos Blog
Cisco Talos Blog
罗磊的独立博客
小众软件
小众软件
阮一峰的网络日志
阮一峰的网络日志
Vercel News
Vercel News

Ben Frain

So, you want a React modal that uses the <dialog> element and transitions in AND out? Scroll indicators on tables with background colours using animation-timeline Review: SoundPEATS Clip1 Open ear clip-on headphones VS Code – highlight just the active indent guide Review: MoErgo Go60, a split ergonomic and fully programmable keyboard Review: Kinesis mWave mechanical ergonomic and programmable keyboard iOS26 Safari theme-color/tab-tinting with fixed position elements is a mess New Book: Responsive Web Design with HTML5 and CSS, 5th Edition Use @supports with a proxy feature/value for features you can’t test for (@starting-style) First adventures in View Transitions Review: Benq Screenbar Pro and Halo lightbars. The kit you never knew you needed! Center items in a container, and make then left aligned when they overflow A single element CSS donut timer/countdown timer, that can sit on any background Review: Open Ear Headphones – Bose Open Ultra v Huawei FreeClip In search of the perfect autocomplete for CSS Managing multiple versions of node, without NVM or additional tools Review: Keychron Q14 Max Alice 96 Key mechanical keyboard NEW VIDEO COURSE: Responsive Web Design with HTML5 and CSS Is CSS Grid really slower than Flexbox? Review: Advantage360 Pro Signature Edition 2024 mechanical ergonomic keyboard More Keys or Fewer Keys for mechanical keyboards Yes! You can use position: sticky and overflow together Neovim – how to do project-wide find and replace? Review: Keyboardio Model 100, split, wooden, mechanical keyboard Struggling to learn SwiftUI How to create rounded gradient borders with any background in CSS How to get equal size icons in the cmp completion menu of Neovim with Kitty terminal Review: Dygma Defy, split, mechanical, programmable ergonomic keyboard What’s the best way to reset WAAPI chained animations? Using CSS @property inside shadowRoot (web components) workaround Dynamically create a ref for items when iterating over them in lit.dev templates Selecting and pausing running animations in Lit Web Components New Web APIs — a popover on top of a dialog element can’t be interacted with? Review: ZSA Voyager, split, mechanical keyboard Russel Brand, narcissism, and a sadly common pattern… When it comes to text editors, I feel like Goldilocks Simple settings for writing and converting markdown with Sublime Text Review: The ZSA Platform tenting kit for the Moonlander keyboard Logitech MX Master 3/3s scroll wheel fix Review: Dell 6K 32″ Monitor U3224KBA I broke my keyboard! Swapping the key switches in the Kinesis Advantage360 Pro HUGE macOS Productivity boost: Set-up simple, keyboard only, instant App switching and arrangement Adding to $PATH for a central location for Neovim/NPM tools Neovim Power Tips: Volume 2 Review: MoErgo Glove80, split, wireless, columnar ergonomic keyboard with RGB Review: Kinesis Advantage 360 Pro — split ergo mechanical keyboard Review: Dactyl Manuform – an ergonomic, custom built mechanical keyboard How to animate along an SVG path at the same time the path animates? Getting the context of Web Components (lit)
Building a line graph with CSS clip-mask
Ben Frain · 2023-06-23 · via Ben Frain

This isn’t a substitute for d3 or chart.js. It is however, a surprisingly simple and effective way of creating a line graph.

I’ve used this clip-path technique when I want to show a basic updating worm of data but don’t want to add an extra library.

I’m using a bunch of stubbed data here to illustrate, and JavaScript to render some of the UI but the crucial thing to understand is CSS. With a little calc() and the use of clip-mask you can easily create a line graph (or worm as they are sometimes known). The premise is simple, a clip-mask clips a rectangle with a series of X/Y coordinates. Each bringing the shape ‘in’ from it’s edges. The best way to understand how the syntax works is to take a look at Bennet Feely’s excellent clippy.

Here is a basic implementation:

Example

See the Pen Untitled by Ben Frain (@benfrain) on CodePen.

Specifics of the technique

And here is the clip-mask creating that line chart:

clip-path: polygon(
  0% 60%,
  20% 90%,
  40% 43.33%,
  60% 61.67%,
  80% 23.33%,
  100% 18.33%,
  100% calc(18.33% - 1px),
  80% calc(23.33% - 1px),
  60% calc(61.67% - 1px),
  40% calc(43.33% - 1px),
  20% calc(90% - 1px),
  0% calc(60% - 1px)
);

The key thing to achieve this effect is understanding that to create a ‘line’, we want to run once along our data to bring our mask in from the top, and then, assuming we want a line and not a filled shape (also possible), we want to bring our mask in from the bottom by going over the positions again, exactly the same in reverse, albeit 1px shorter, made easy with calc(), in the vertical axis. You can see this clearly when looking at the mask above, where it is laid out one position at a time.

Implementation

How you manipulate your data to create this string is up to you. For this basic example, from a basic array of data shaped like this:

let values = [
  {
    time: 1,
    value: 144,
  },
  {
    time: 2,
    value: 126,
  },
  // ... more
];

I have used a function like so:

function makeWorm() {
  let duration = 150;
  let generatePointX = (data) => (100 / duration) * data.time;
  let generatePointY = (data) => ((data.value - 120) * 100) / (180 - 120);

  let maskString = `clip-path: polygon(`;
  values.map((data) => {
    maskString += `${generatePointX(data).toFixed(2)}% ${(
      100 - generatePointY(data)
    ).toFixed(2)}%, `;
  });
  values.toReversed().map((data, i, { length }) => {
    maskString += `${generatePointX(data).toFixed(2)}% calc(${(
      100 - generatePointY(data)
    ).toFixed(2)}% - 1px)`;
    if (i + 1 !== length) {
      maskString += `, `;
    }
  });

  maskString += `);`;
  return maskString;
}

worm.style = makeWorm();

There are a couple of hard coded values in there for the vertical points, based upon the values I have in my data. You would likely want to compute the bottom and top extremes of your chart based upon the highest and lowest values in your own data.

There are likely cleaner ways of generating the string too but this works; looping over the array of data, first one way for the ‘top’ positions of the mask, and then reversing through the array to generate the ‘bottom’ coordinates.

Summary

This way of creating a line chart is very basic but very light. Sure, we can do this kind of thing with the more suitable SVG but I found it a fun application of clip-mask. It also isn’t ‘transition-able’, so if you were hoping of getting it cleanly animating in this manner, I’m afraid you’re out of luck.

Improvements? Examples of clip-mask graphs you’ve made yourself? Let me know in the comments below.