Scan Lines

Developer
Size
3,012 Kb
Views
6,072

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 Previews

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);
}
Scan Lines - Script Codes
Scan Lines - Script Codes
Home Page Home
Developer A S P
Username asp
Uploaded January 15, 2023
Rating 3
Size 3,012 Kb
Views 6,072
Do you need developer help for Scan Lines?

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!

A S P (asp) Script Codes
Create amazing art & images 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!