Canvas Vasarely

Size
3,290 Kb
Views
24,288

How do I make an canvas vasarely?

I ran into a print by Victor Vasarely and wanted to try to recreate it in canvas. . What is a canvas vasarely? How do you make a canvas vasarely? This script and codes were developed by Sakri Rosenstrom on 13 September 2022, Tuesday.

Canvas Vasarely Previews

Canvas Vasarely - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Canvas Vasarely</title> <link rel="stylesheet" href="css/style.css">
</head>
<body> <!-- I ran into a Vicotor Vasarely print the other day, thought I would try to make it in canvas. Based on this: https://i.ytimg.com/vi/P6Vina_qNBw/maxresdefault.jpg Not too shabby for ~140 lines :)
--> <script src="js/index.js"></script>
</body>
</html>

Canvas Vasarely - Script Codes CSS Codes

html, body{ text-align: center; background-color: #9e9e9e; overflow: hidden; margin: 0px; padding: 0px;
}

Canvas Vasarely - Script Codes JS Codes

function normalize(value, min, max){ return (value - min) / (max - min);
}
function interpolate(normValue, min, max){ return min + (max - min) * normValue;
}
function easeInSine(normal) { return 1.0 - Math.cos(normal * halfPi);
}
function easeOutSine(normal) { return Math.sin(normal * halfPi);
}
function easeInOutSine(normal) { normal = normal * 2; return (normal < 1) ? 0.5 * easeInSine(normal) : 0.5 * easeOutSine(normal - 1) + 0.5;
}
var halfPi = Math.PI/2, pi2 = Math.PI*2;
var canvas, context, circle = {}, cells = 20, cellSize, margin, start, duration;
var backgroundImage = new Image();
var bgColor = "#9e9e9e", bgBubbleColor = "#000000";
var controlPoints = [], anchorPoints = [];//no need to instantiate every iteration (helps?)
window.onload = function(){ canvas = document.createElement("canvas"); document.body.appendChild( canvas ); context = canvas.getContext("2d"); canvas.width = canvas.height = Math.min(window.innerWidth, window.innerHeight); cellSize = Math.floor(canvas.width / cells); canvas.width = canvas.height = canvas.width - canvas.width % cellSize; margin = Math.floor(cellSize * .057); createBackground(); resetAnimation(); loop();
}
function resetAnimation(){ start = Date.now(); duration = 6000 + Math.round(Math.random() * 4000);//min 6 - max 10 seconds
}
function loop(){ if(Date.now() - start >= duration){ resetAnimation(); } var percent = normalize(Date.now() - start, 0, duration); circle.x = interpolate(easeInOutSine(percent), start % canvas.width, (start + duration) % canvas.width ); circle.y = interpolate(easeInOutSine(percent), start % canvas.height, (start + duration) % canvas.height); circle.radius = canvas.height * .5 + Math.cos(Date.now() % 10000 / 10000 * pi2 ) * canvas.height * .25; context.drawImage(backgroundImage, 0, 0); renderPoints(); requestAnimationFrame(loop);
}
function inCircle(circle, x, y){ return Math.pow(x - circle.x, 2) + Math.pow(y - circle.y, 2) < circle.radius * circle.radius;
}
function renderPoints(){ context.beginPath(); var gradientFill = context.createRadialGradient(circle.x, circle.y, 0, circle.x, circle.y, circle.radius); gradientFill.addColorStop(0.25, 'rgba(253, 247, 49, 1.000)'); gradientFill.addColorStop(0.6, 'rgba(251, 175, 3, 1.000)'); gradientFill.addColorStop(0.85, 'rgba(89, 68, 11, 1.000)'); gradientFill.addColorStop(1, 'rgba(0, 0, 0, 1.000)'); context.fillStyle = gradientFill; var startRow = Math.max(0, Math.floor((circle.y - circle.radius) / cellSize)); var startCol = Math.max(0, Math.floor((circle.x - circle.radius) / cellSize)); var endRow = Math.min(cells, Math.ceil((circle.y + circle.radius) / cellSize) ); var endCol = Math.min(cells, Math.ceil((circle.x + circle.radius) / cellSize) ); var startIndex = startRow * cells + startCol; var endIndex = (endRow * cells + endCol); var cellX, cellY; for(var i=startIndex; i<endIndex; i++){ cellX = (i % cells) * cellSize; cellY = Math.floor(i / cells) * cellSize; renderBlobInCircle(cellX, cellY); } context.fill();
}
function renderBlobInCircle(cellX, cellY){ var i, render = false, angle, x,y, targetX,targetY, xNormal,yNormal; for(i=0; i<4; i++){ x = i%3 ? cellX + cellSize - margin : cellX + margin; y = i>1 ? cellY + cellSize - margin : cellY + margin; if(inCircle(circle, x, y)){ angle = Math.atan2(y - circle.y, x - circle.x); targetX = circle.x + Math.cos(angle) * circle.radius; targetY = circle.y + Math.sin(angle) * circle.radius; xNormal = normalize(x, circle.x, targetX); yNormal = normalize(y, circle.y, targetY); controlPoints[i*2] = interpolate(Math.sin(xNormal * halfPi), x, targetX); controlPoints[i*2 + 1] = interpolate(Math.sin(yNormal * halfPi), y, targetY); render = true; }else{ controlPoints[i*2] = x; controlPoints[i*2 + 1] = y; } } if(render){ context.clearRect(cellX, cellY, cellSize, cellSize); renderBlob(); }
}
function renderBlob(){ for(var i=0; i<8; i++){ anchorPoints[i] = interpolate(.5, controlPoints[i], controlPoints[(i+2) % 8]); } context.moveTo(anchorPoints[0], anchorPoints[1]); context.quadraticCurveTo(controlPoints[2], controlPoints[3], anchorPoints[2], anchorPoints[3]); context.quadraticCurveTo(controlPoints[4], controlPoints[5], anchorPoints[4], anchorPoints[5]); context.quadraticCurveTo(controlPoints[6], controlPoints[7], anchorPoints[6], anchorPoints[7]); context.quadraticCurveTo(controlPoints[0], controlPoints[1], anchorPoints[0], anchorPoints[1]);
}
function createBackground(){ context.fillStyle = bgColor; context.fillRect(0,0, canvas.width, canvas.height); context.beginPath(); context.fillStyle = bgBubbleColor; var i, x, y, total = cells * cells; for(i=0; i<total; i++){ x = i % cells * cellSize; y = Math.floor(i / cells) * cellSize; controlPoints = [ x + margin, y + margin, x + cellSize - margin, y + margin, x + cellSize - margin, y + cellSize -margin, x + margin, y + cellSize -margin ]; renderBlob(); } context.fill(); backgroundImage.src = canvas.toDataURL(); context.clearRect(0, 0, canvas.width, canvas.height);
}
Canvas Vasarely - Script Codes
Canvas Vasarely - Script Codes
Home Page Home
Developer Sakri Rosenstrom
Username sakri
Uploaded September 13, 2022
Rating 3.5
Size 3,290 Kb
Views 24,288
Do you need developer help for Canvas Vasarely?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Sakri Rosenstrom (sakri) Script Codes
Create amazing Facebook ads with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!