Scan Lines
How do I make an scan lines?
Reproduce an image using horizontal lines built from CSS gradients.. What is a scan lines? How do you make a scan lines? This script and codes were developed by A S P on 15 January 2023, Sunday.
Scan Lines - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Scan Lines</title> <link rel="stylesheet" href="css/style.css">
</head>
<body>
<button id="run" onclick="run()">Run</button>
<input id="threshold" type="range" min="0" max="255"/>
<div id="thresholddisplay">--</div>
<h1>Source</h1>
<canvas id="source" height="200" width="200"></canvas>
<h1>Destination</h1>
<canvas id="destination" height="200" width="200"></canvas>
<div id="destinationdiv"></div> <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script> <script src="js/index.js"></script>
</body>
</html>
Scan Lines - Script Codes CSS Codes
#source, #destination, #destinationdiv { border: 2px #000 solid; height: 200px; width: 200px;
}
#destination
{ display: none;
}
#run, #threshold { width: 200px; cursor: pointer;
}
#run
{ display: none;
}
#thresholddisplay
{ text-align: center; width: 200px;
}
.line
{ width: 200px; height: 1px;
}
Scan Lines - Script Codes JS Codes
var height = 200;
var width = 200;
var threshold = 10;
var isprocessing = false;
var tinput = $("#threshold");
tinput.on('change', function() { //console.log('changed'); if (!isprocessing) { isprocessing = true; threshold = tinput.val(); $("#thresholddisplay").html(threshold); run(); isprocessing = false; }
})
var imgsrc = 'http://annieperrier.com/code/images/baseball_128x128.png';
var scanvas = document.getElementById('source');
var sctx = scanvas.getContext('2d');
var dcanvas = document.getElementById('destination');
var dctx = dcanvas.getContext('2d');
// draw a white rect to cover area so transparent aren't black
sctx.fillStyle="#FFFFFF";
sctx.fillRect(0,0,width,height);
var divd = $("#destinationdiv");
var imageObj = new Image();
// This makes it work in codepen
imageObj.crossOrigin = "Anonymous";
imageObj.onload = function() { sctx.drawImage(imageObj, 0, 0); tinput.val(threshold).trigger('change');
};
imageObj.src = imgsrc;
function run()
{ var imgData = sctx.getImageData(0,0,width,height); //console.log(imgData); dctx.putImageData(imgData,0,0); divd.html(''); var r, g, b; var hex; var line, cur, prev, prevj; var i, j; for (i = 0; i < height; i++) { line = [], cur = [], prev = null; for (j = 0, k = 0; j < width; j++, k+=4) { r = imgData.data[(width * i + j) * 4]; g = imgData.data[(width * i + j) * 4+1]; b = imgData.data[(width * i + j) * 4+2]; //var a = imgData.data[(width * i + j) * 4+3]; cur = [r, g, b]; if (!prev || exceedthreshold(prev, cur)) { // add a pixel at the stop to prevent bleed if (j != 0 && prevj != j-1) { hex = rgbToHex(prev[0], prev[1], prev[2]); line.push([hex, j-1]); } hex = rgbToHex(r, g, b); //console.log(hex); line.push([hex, j]); prev = [r, g, b]; prevj = j; } k += 4; } addline(line); }
}
function addline(gradients)
{ // convert stops //#1e5799 0px,#2989d8 50px,#2989d8 150px,#7db9e8 200px var stops = ''; for (var i = 0; i < gradients.length; i++) { stops += gradients[i][0]+" "+gradients[i][1]+"px"; if (gradients.length-1 > i) stops += ", "; } var d = $("<div/>").addClass('line'); //console.log(stops); d.css('background', 'linear-gradient(to right, '+stops+')'); divd.append(d);
}
function exceedthreshold(prev, cur)
{ for (var i = 0; i < 3; i++) { if (Math.abs(prev[i] - cur[i]) > threshold) return true; } return false;
}
// http://stackoverflow.com/questions/5623838/rgb-to-hex-and-hex-to-rgb
function rgbToHex(r, g, b)
{ return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
}
Developer | A S P |
Username | asp |
Uploaded | January 15, 2023 |
Rating | 3 |
Size | 3,012 Kb |
Views | 6,072 |
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!
Name | Size |
Snowflake Scene | 3,306 Kb |
Background Patterns | 1,888 Kb |
Grid with Fixed and Resizable Columns | 2,857 Kb |
Header Underline | 1,782 Kb |
SVG Alien | 2,107 Kb |
Button Pulse | 2,377 Kb |
Dr. Puyo | 4,854 Kb |
Monster Name Generator | 2,582 Kb |
SVG Marvin | 3,030 Kb |
Laser Cat | 2,035 Kb |
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!
Name | Username | Size |
Slide In Panel | Vikvarg | 2,811 Kb |
Javascript Welcome | Peterlewicki | 1,573 Kb |
Drag in vanilla js using dotval math instead of translate | Paulq | 2,662 Kb |
A Pen by Jess | Jessamyne | 5,100 Kb |
Count checked checkboxes with jQuery | Mestika | 2,343 Kb |
Get third wednesday | Wojtek1150 | 2,691 Kb |
Project MMetro beta | Atomicsong | 5,157 Kb |
Countdown with Rings | Ewganoel | 2,490 Kb |
Testing Code Pen | Jduprey | 1,468 Kb |
Starting out with Ember.JS | Cfleschhut | 4,808 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!