Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="container" class="borderimg"></div>
              
            
!

CSS

              
                html, body, div {
  margin: 0;
  padding: 0;
  border: 0;
}
body {
  overflow: hidden;
  background-image: url("https://www.nkunited.de/ExternalImages/jsfiddle/wallPattern.png");
  background-repeat: repeat;
}
.borderimg {
  border: 106px solid transparent;
  padding: 0px;
  -webkit-border-image: url(https://www.nkunited.de/ExternalImages/jsfiddle/ImageFrame.png) 37 round; /* Safari 3.1-5 */
  -o-border-image: url(https://www.nkunited.de/ExternalImages/jsfiddle/ImageFrame.png) 37 round; /* Opera 11-12.1 */
  border-image: url(https://www.nkunited.de/ExternalImages/jsfiddle/ImageFrame.png) 37 round;
}

#container {
	width: 512px;
  height: 512px;
	background-color: #fff;
	position: absolute;
  left:0; right:0;
  top:0; bottom:0;
	margin:auto;
  text-align: center;
}
              
            
!

JS

              
                //---

var w = 512;
var h = 512;

var canvas;
var context;

var image;

var imageData;
var data;

var mouseActive = false;
var mouseDown = false;
var mousePos = { x:0, y:0 };
var mouseMaxDist = 100;

var inputCanvas;
var inputContext;
var inputImageData;
var inputData;

var lines = {};
var linesCount = 0;
var lineFrequency = 0.18;
var lineSegments = 128;
var lineSpeed = 0.015;

var thresholdValue = 128;

var timer = 0;

//---

function init() {

  canvas = createCanvas( w, h, 'container' );
  canvas.addEventListener( 'mousedown', mouseDownHandler, false );
  canvas.addEventListener( 'mouseup', mouseUpHandler, false );
  canvas.addEventListener( 'mousemove', mouseMoveHandler, false );
  canvas.addEventListener( 'mouseenter', mouseEnterHandler ); 
  canvas.addEventListener( 'mouseleave', mouseLeaveHandler ); 

  context = canvas.getContext( '2d' );
  context.fillStyle = '#000000';
  context.fillRect( 0, 0, w, h );

  imageData = context.getImageData( 0, 0, w, h );
  data = imageData.data;
  
  loadImage( 'https://nkunited.de/ExternalImages/jsfiddle/MonaLisa05.png', 'anonymous' );

};

function createCanvas( width, height, containerId ) {

  var canvas = document.createElement( 'canvas' );
      canvas.width = width;
  		canvas.height = height;
  
  var container = document.getElementById( containerId );
      container.appendChild( canvas );
      
  return canvas;

};

//---

function loadImage( src, crossOrigin ) {

	image = document.createElement( 'IMG' );
  image.onload = function () {
    
    saveImageData();

    animate();
        
  };
  
	image.crossOrigin = crossOrigin;
	image.src = src;

};

function saveImageData() {

	inputCanvas = document.createElement( 'canvas' );
	inputCanvas.width = w;
	inputCanvas.height = h;
	inputContext = inputCanvas.getContext( '2d' );
  
  inputContext.drawImage( image, 0, 0 );
	inputImageData = inputContext.getImageData ( 0, 0, w, h );
  inputData = inputImageData.data;
  
};

//---

function clearImageData() {

    for ( var i = 0, l = data.length; i < l; i += 4 ) {

        data[ i ] = 0;
        data[ i + 1 ] = 0;
        data[ i + 2 ] = 0;
        data[ i + 3 ] = 255;

    }

};

function setPixel( x, y, r, g, b, a ) {

    var i = ( x + y * imageData.width ) * 4;

    data[ i ] = r;
    data[ i + 1 ] = g;
    data[ i + 2 ] = b;
    data[ i + 3 ] = a;

};

function getPixel( data, x, y ) {

    var i = ( x + y * imageData.width ) * 4;
    
    var pixel = {};

    pixel.r = data[ i ];
    pixel.g = data[ i + 1 ];
    pixel.b = data[ i + 2 ];
    pixel.a = data[ i + 3 ];
    
    return pixel;

};

//---

function drawLine( x1, y1, x2, y2, r, g, b, a ) {

  var dx = Math.abs( x2 - x1 );
  var dy = Math.abs( y2 - y1 );

  var sx = ( x1 < x2 ) ? 1 : -1;
  var sy = ( y1 < y2 ) ? 1 : -1;

  var err = dx - dy;

  var lx = x1;
  var ly = y1;    

  while ( true ) {

    if ( lx > 0 && lx < w && ly > 0 && ly < h ) {

      setPixel( lx, ly, r, g, b, a );

    }

    if ( ( lx === x2 ) && ( ly === y2 ) )
      break;

    var e2 = 2 * err;

    if ( e2 > -dx ) { 

      err -= dy; 
      lx += sx; 

    }

    if ( e2 < dy ) { 

      err += dx; 
      ly += sy; 

    }

  }

};

//---

function addLineSegment( x, y, speed ) {

  var lineSegment = {};
      lineSegment.x = x;
      lineSegment.y = y;
      lineSegment.speed = speed;

  return lineSegment;

};

function addLine( x, yTop, yBottom, segments, speed ) {

	var line = {};
  		line.segments = [];
      line.id = linesCount;
      
  linesCount++;

	var steps = Math.floor( ( yBottom - yTop ) / segments );

	for ( var y = yTop; y < yBottom; y += steps ) {
  
  	line.segments.push( addLineSegment( x, y, speed ) );
  
  }
  
  lines[ line.id ] = line;

};

/*
function addLines( number, segments, speed ) {

	for ( var x = 0; x < number; x++ ) {
  
  	//console.log(( w / number ) * x);
  
  	lines.push( addLine( Math.round( ( w / number ) * x ), 0, h, segments, speed ) );
  
  }

};
*/

//---

/*
function onResize(){
  	
  w = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
  h = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;

	center2D = { x:w / 2, y:h / 2 };

  canvas.width = w;
  canvas.height = h;

  context.fillStyle = '#000000';
  context.fillRect( 0, 0, w, h );

  imageData = context.getImageData( 0, 0, w, h );
  data = imageData.data;

};
*/
//---

function render() {

	timer += lineFrequency;
  
  if ( timer >= 1 ) {
  
		timer = 0;
    
    addLine( 0, 0, h, lineSegments, lineSpeed );
  
  }
  
  for ( var key in lines ) {

    var line = lines[ key ];
    var resetCount = 0;
    
    for ( var j = 0; j < line.segments.length; j++ ) {

      var lineSegment = line.segments[ j ];
      
      var distance = 0;
      
      if ( mouseActive === false ) {
      
      	distance = mouseMaxDist + 1;
      
      } else {
      
      	distance = getDistance( lineSegment, mousePos );
      
      }

      var color = getPixel( inputData, lineSegment.x | 0, lineSegment.y | 0 );

			var thresholdSub = ( color.r + color.g + color.b ) / 3;
      var thresholdMain = thresholdSub / 100;

      if ( j === 0 ) {

        if ( thresholdMain < 0.5 ) {

          thresholdMain = 0.5;

        }

        lineSegment.x += thresholdMain;

      } else {

        var lineSegmentMain = line.segments[ 0 ];

        if ( thresholdSub > thresholdValue ) {
        
        	if ( mouseDown ) {
          
          	lineSegment.speed += 0.025;
          
          } else {
          
          	lineSegment.speed -= 0.025;
          
          }
          
        } else {

          if ( mouseDown ) {
          
          	lineSegment.speed -= 0.025;
          
          } else {
          
          	lineSegment.speed += 0.025;
          
          }

        }
        
        var speedMin = 0.04;
        var speedMax = 0.15;
        
        if ( distance > mouseMaxDist ) {

          if ( lineSegment.speed < speedMin ) {

            lineSegment.speed = speedMin;

          }

          if ( lineSegment.speed > speedMax ) {

            lineSegment.speed = speedMax;

          }
          
        } else {
        
        	var percent = distance / mouseMaxDist;
          
          if ( percent < 0.25 ) {
          
          	percent = 0.25;
          
          } 
          
          if ( mouseDown ) {
          
          	lineSegment.speed = speedMax * percent;
          
          } else {
          
          	lineSegment.speed = speedMin * percent;
          
          }
        
        }

        lineSegment.x += ( lineSegmentMain.x - lineSegment.x ) * lineSegment.speed;

      }

      //---

      var lineSegmentLineTo;

      if ( j > 1 ) {

        lineSegmentLineTo = line.segments[ j - 1 ];

        //drawLine( lineSegment.x | 0, lineSegment.y | 0, lineSegmentLineTo.x | 0, lineSegmentLineTo.y | 0, 255, 255, 255, 255 );
        drawLine( lineSegment.x | 0, lineSegment.y | 0, lineSegmentLineTo.x | 0, lineSegmentLineTo.y | 0, color.r, color.g, color.b, 255 );

      } 

      if ( j === 1 ) {

        lineSegmentLineTo = line.segments[ 1 ];

        drawLine( lineSegment.x | 0, lineSegment.y | 0, lineSegmentLineTo.x | 0, 0, color.r, color.g, color.b, 255 );

      }

      if ( lineSegment.x >= w ) {

        resetCount++;

      }

    }

    if ( resetCount === line.segments.length ) {

      delete lines[ line.id ];

    }

  }

};

//---

function getDistance( v1, v2 ) {

  var dx = v2.x - v1.x;
  var dy = v2.y - v1.y;
  
  return Math.sqrt( dx * dx + dy * dy );

};

//---

function mouseDownHandler( event ) {

	mouseDown = true;

};

function mouseUpHandler( event ) {

  mouseDown = false;

};

function mouseMoveHandler( event ) {

  mousePos = getMousePos( canvas, event );

};

function mouseEnterHandler( event ) {

  mouseActive = true;

};

function mouseLeaveHandler( event ) {

  mouseActive = false;

  mouseDown = false;

};

//---

function getMousePos( canvas, event ) {

  var rect = canvas.getBoundingClientRect();

  return { x:event.clientX - rect.left, y:event.clientY - rect.top };

};

//---

function animate() {

  clearImageData();
  
  render();
  
  context.putImageData( imageData, 0, 0 );

  requestAnimationFrame( animate );

};

window.requestAnimFrame = ( function() {

    return  window.requestAnimationFrame       ||
            window.webkitRequestAnimationFrame ||
            window.mozRequestAnimationFrame    ||
            function( callback ) {
                window.setTimeout( callback, 1000 / 60 );
            };

} )();

//---

init();
              
            
!
999px

Console