JSFiddle - React, Tailwind, and code Playground
by realhunts
HTML
<script>
/*
imagetracer.js version 1.2.6
Simple raster image tracer and vectorizer written in JavaScript.
[email protected]
*/
/*
The Unlicense / PUBLIC DOMAIN
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to http://unlicense.org/
*/
(function(){ 'use strict';
function ImageTracer(){
var _this = this;
this.versionnumber = '1.2.6',
////////////////////////////////////////////////////////////
//
// API
//
////////////////////////////////////////////////////////////
// Loading an image from a URL, tracing when loaded,
// then executing callback with the scaled svg string as argument
this.imageToSVG = function( url, callback, options ){
options = _this.checkoptions(options);
// loading image, tracing and callback
_this.loadImage(
url,
function(canvas){
callback(
_this.imagedataToSVG(...
JavaScript
/* // Loading smiley.png, tracing and calling alert callback on the SVG string result
ImageTracer.imageToSVG( 'https://cdn.glitch.com/6b59148a-de9f-4829-98fe-67e50d7ee457%2Fobject_cube.png?v=1591832203766', alert );
// Almost the same with options, and the ImageTracer.appendSVGString callback will append the SVG
ImageTracer.imageToSVG( 'https://cdn.glitch.com/6b59148a-de9f-4829-98fe-67e50d7ee457%2Fobject_cube.png?v=1591832203766', ImageTracer.appendSVGString, { ltres:0.1, qtres:1, scale:10, strokewidth:5 } );
// This uses the 'posterized2' option preset and appends the SVG to an element with id="svgcontainer"
ImageTracer.imageToSVG(
'https://cdn.glitch.com/6b59148a-de9f-4829-98fe-67e50d7ee457%2Fobject_cube.png?v=1591832203766',
function(svgstr){ ImageTracer.appendSVGString( svgstr, 'svgcontainer' ); },
'posterized2'
);
*/
// The helper function loadImage() loads an image to a canvas, then executing callback:
// appending the canvas to a div here.
/* ImageTracer.loadImage(
'https://cdn.glitch.com/6b59148a-de9f-4829-98fe-67e50d7ee457%2Fobject_cube.png?v=1591832203766',
function(canvas){ (document.getElementById('canvascontainer')).appendChild(canvas); }
); */
function download(filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
// ImageData can be traced to an SVG string synchronously.
ImageTracer.loadImage(
'https://cdn.glitch.com/6b59148a-de9f-4829-98fe-67e50d7ee457%2Fobject_cube.png?v=1591832203766',
function(canvas){
// Getting ImageData from canvas with the helper function getImgdata().
var imgd = ImageTracer.getImgdata( canvas );
// Synchronous tracing to SVG string
var svgstr = ImageTracer.imagedataToSVG( imgd, { scale:.5 } );
...