shaneosullivan / example-canvas-fill Goto Github PK
View Code? Open in Web Editor NEWLicense: MIT License
License: MIT License
Here is a faster floodfill implementation. The main differences are:
To run:
airplane.png
into the same directory.python -m http.server
.The largest area takes about 7 ms to flood fill on my laptop (after initial warmup).
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Floodfill</title>
</head>
<body>
<canvas id="canvas"></canvas>
<img id="image" src="airplane.png" style="display: none;">
<div>
<div style="background-color: rgba(0, 255, 0, 1); width: 50px; height: 50px; display: inline-block;" onclick="setColor(this)"></div>
<div style="background-color: rgba(255, 0, 0, 1); width: 50px; height: 50px; display: inline-block;" onclick="setColor(this)"></div>
<div style="background-color: rgba(0, 0, 255, 1); width: 50px; height: 50px; display: inline-block;" onclick="setColor(this)"></div>
<div style="background-color: rgba(255, 255, 0, 1); width: 50px; height: 50px; display: inline-block;" onclick="setColor(this)"></div>
<div style="background-color: rgba(0, 255, 255, 1); width: 50px; height: 50px; display: inline-block;" onclick="setColor(this)"></div>
<div style="background-color: rgba(255, 0, 255, 1); width: 50px; height: 50px; display: inline-block;" onclick="setColor(this)"></div>
</div>
<script>
function floodfill(rgba, width, height, x, y, color){
// Floodfill an RGBA image array of a given width and height
// starting at (x, y) with a color.
var visited = new Uint8Array(width * height);
var queue = new Int32Array(2 * width * height);
// Only non-black pixels can be visited
for (var i = 0; i < width * height; i++){
var r = rgba[i * 4 + 0];
var g = rgba[i * 4 + 1];
var b = rgba[i * 4 + 2];
var a = rgba[i * 4 + 3];
var isBlack = a > 100 && r + g + b == 0
visited[i] = isBlack;
}
// Add initial pixel to queue
var n = 0;
queue[n++] = x;
queue[n++] = y;
// Mark initial pixel as visited
var i = x + y * width;
visited[i] = 1;
rgba[i * 4 + 0] = color[0];
rgba[i * 4 + 1] = color[1];
rgba[i * 4 + 2] = color[2];
rgba[i * 4 + 3] = 255;
// While we have not processed all pixels yet
while (n > 0){
// Pop pixel from queue
var y = queue[--n];
var x = queue[--n];
// Scan to the left
var x1 = x;
while (x1 > 0 && !visited[x1 - 1 + y * width]) x1--;
// Scan to the right
var x2 = x;
while (x2 < width - 1 && !visited[x2 + 1 + y * width]) x2++;
// Mark all pixels in scan line as visited
for (var x = x1; x <= x2; x++){
var i = x + y * width
visited[i] = 1;
rgba[i * 4 + 0] = color[0];
rgba[i * 4 + 1] = color[1];
rgba[i * 4 + 2] = color[2];
rgba[i * 4 + 3] = 255;
}
// Add pixels above scan line to queue
if (y + 1 < height){
for (var x = x1; x <= x2; x++){
var i = x + (y + 1) * width;
if (!visited[i]){
visited[i] = 1;
queue[n++] = x;
queue[n++] = y + 1;
}
}
}
// Add pixels below scan line to queue
if (y > 0){
for (var x = x1; x <= x2; x++){
var i = x + (y - 1) * width;
if (!visited[i]){
visited[i] = 1;
queue[n++] = x;
queue[n++] = y - 1;
}
}
}
}
}
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
var image = document.getElementById('image');
image.onload = function(){
canvas.width = image.width;
canvas.height = image.height;
context.drawImage(image, 0, 0);
}
var color = [0, 255, 0];
function setColor(element){
color = element.style.backgroundColor.match(/\d+/g).map(Number);
}
canvas.onmousedown = function(e){
var rect = canvas.getBoundingClientRect();
var x = e.clientX - rect.left | 0;
var y = e.clientY - rect.top | 0;
var imageData = context.getImageData(0, 0, canvas.width, canvas.height);
var rgba = imageData.data;
var startTime = window.performance.now();
floodfill(rgba, canvas.width, canvas.height, x, y, color);
var elapsedTime = window.performance.now() - startTime;
console.log('Time: ' + elapsedTime + ' ms');
context.putImageData(imageData, 0, 0);
}
</script>
</body>
</html>
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.