<div class="box" style="--img:url(https://i.pinimg.com/736x/8f/a0/51/8fa051251f5ac2d0b756027089fbffde--terry-o-neill-al-pacino.jpg)">Al Pacino</div>
<div class="box" style="--img:url(https://i.pinimg.com/originals/28/d2/e6/28d2e684e7859a0dd17fbd0cea00f8a9.jpg)">Ben Stiller</div>
<div class="box" style="--img:url(https://i.pinimg.com/originals/ee/85/08/ee850842e68cfcf6e3943c048f45c6d1.jpg)">Patrick Stewart</div>
<script>
if (CSS.paintWorklet) { CSS.paintWorklet.addModule('/t_afif/pen/ZEyLWgp.js');
} else {
alert("Your browser cannot run this demo. Consider Chrome or Edge instead")
}
</script>
<!-- images from https://codepen.io/petegarvin1/pen/YzWBbRx -->
@property --border{
syntax: '<length>';
inherits: true;
initial-value: 0;
}
.box {
width:250px;
height:250px;
margin:5px;
cursor:pointer;
--path:50% 0%, 90% 20%, 100% 60%, 75% 100%, 25% 100%, 0% 60%, 10% 20%;
clip-path:polygon(var(--path));
--border:120px;
transition:--border .5s;
position:relative;
background:#444;
color:#fff;
display:inline-grid;
place-content:center;
font-size:20px;
font-weight:bold;
font-family:sans-serif;
}
.box:before {
content:"";
position:absolute;
background:var(--img) center/cover;
inset:0;
-webkit-mask:paint(polygon-border);
}
.box:hover {
--border:15px;
}
body {
background:#f2f2f2;
}
registerPaint('polygon-border', class {
static get inputProperties() {
return [
'--path',
'--border',
'--dash'
]
}
paint(ctx, size, properties) {
const points = properties.get('--path').toString().split(',');
const b = parseFloat(properties.get('--border').value);
const d = properties.get('--dash').toString().split(',');
const w = size.width;
const h = size.height;
const cc = function(x,y) {
var fx=0,fy=0;
if (x.indexOf('calc') > -1) {
var tmp = x.replace('calc(','').replace(')','');
if (tmp.indexOf('+') > -1) {
tmp = tmp.split('+');
fx = (parseFloat(tmp[0])/100)*w + parseFloat(tmp[1]);
} else {
tmp = tmp.split('-');
fx = (parseFloat(tmp[0])/100)*w - parseFloat(tmp[1]);
}
} else if (x.indexOf('%') > -1) {
fx = (parseFloat(x)/100)*w;
} else if(x.indexOf('px') > -1) {
fx = parseFloat(x);
}
if (y.indexOf('calc') > -1) {
var tmp = y.replace('calc(','').replace(')','');
if (tmp.indexOf('+') > -1) {
tmp = tmp.split('+');
fy = (parseFloat(tmp[0])/100)*h + parseFloat(tmp[1]);
} else {
tmp = tmp.split('-');
fy = (parseFloat(tmp[0])/100)*h - parseFloat(tmp[1]);
}
} else if (y.indexOf('%') > -1) {
fy = (parseFloat(y)/100)*h;
} else if(y.indexOf('px') > -1) {
fy = parseFloat(y);
}
return [fx,fy];
}
var p = points[0].trim().split(/(?!\(.*)\s(?![^(]*?\))/g);
p = cc(p[0],p[1]);
ctx.beginPath();
ctx.setLineDash(d);
ctx.moveTo(p[0],p[1]);
for (var i = 1; i < points.length; i++) {
p = points[i].trim().split(/(?!\(.*)\s(?![^(]*?\))/g);
p = cc(p[0],p[1]);
ctx.lineTo(p[0],p[1]);
}
ctx.closePath();
ctx.lineWidth = 2*b;
ctx.strokeStyle = '#000';
ctx.stroke();
}
})
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.