cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmloctocatspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

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.

            
              <a href='javascript:(function(){var t;t=function(){function t(){}return t.prototype.run=function(){return this.getImages(),this.buildCanvas(),this.process(),this.clean()},t.prototype.getImages=function(){return this.images=document.getElementsByTagName("img")},t.prototype.buildCanvas=function(){return this.canvas=document.createElement("canvas"),this.stage=this.canvas.getContext("2d"),document.body.appendChild(this.canvas),this.resizeCanvas(1,1)},t.prototype.resizeCanvas=function(t,e){return this.canvas.setAttribute("style","width: "+t+"px; height: "+e+"px; position: fixed; opacity: 0; left: -"+t+"px; top: -"+e+"px;"),this.canvas.setAttribute("width",t),this.canvas.setAttribute("height",e)},t.prototype.process=function(){var t,e,i,a,s,n;for(s=this.images,n=[],i=0,a=s.length;a>i;i++){e=s[i];try{this.image=e,this.allow(),this.setScale(),this.getImageData(),this.glitchData(),n.push(this.setData())}catch(r){t=r,n.push(console.log(t))}}return n},t.prototype.allow=function(){return this.image.setAttribute("crossOrigin","anonymous")},t.prototype.setScale=function(){var t,e;return e=this.image.offsetWidth,t=this.image.offsetHeight,this.resizeCanvas(e,t)},t.prototype.getImageData=function(){return this.stage.drawImage(this.image,0,0),this.data=this.canvas.toDataURL("image/jpeg")},t.prototype.glitchData=function(){var t,e,i,a,s;for(s=this.data.substring(0,this.data.indexOf(",")+1),e=this.data.substring(this.data.indexOf(",")+1),t=0,i=1+Math.floor(10*Math.random());i>t;)a=Math.round(Math.random()*e.length-1),e=e.substr(0,a)+e.charAt(a+1)+e.charAt(a)+e.substr(a+2),t++;return this.data=s+e},t.prototype.setData=function(){var t;return t=document.createElement("img"),t.setAttribute("width",this.image.offsetWidth),t.setAttribute("height",this.image.offsetHeight),t.setAttribute("src",this.data),document.body.insertBefore(t,this.image),this.image.parentNode.removeChild(this.image)},t.prototype.clean=function(){return this.canvas.parentNode.removeChild(this.canvas),this.image=null,this.data=null},t}(),window.glitch=new t,glitch.run()}).call(this);' class="bookmarklet">Glitch</a>
<p>Drag the button above into your bookmarks, open any image in a new tab, and then click the bookmark to glitch the image.<br /><span>(Right click on the background image and open it in a new tab to try it out.)</span></p>
<img src="http://i.ytimg.com/vi/g2ag8t7AvX8/maxresdefault.jpg" />
            
          
!
            
              @import compass

html , body
  overflow: hidden
  position: absolute
  height: 100%
  width: 100%
  left: 0
  top: 0

html
  background-image: url( http://i.imgur.com/Cceecdk.jpg )
  background-position: left center
  background-size: cover
  
body
  background-color: rgba( 0 , 0 , 0 , 0.3 )
  
img
  position: absolute
  opacity: 0
  width: 100%
  height: 100%
  left: 0
  top: 0

p , .bookmarklet
  text-shadow: 0 0 15px rgba( 0, 0, 0, 0.4 )
  font-weight: 100
  letter-spacing: 0.05em
  font-family: sans-serif
  text-align: center
  position: absolute
  z-index: 2
  color: white
  left: 50%
  top: 50%
  
p
  @include translate( -50% , 0 )
  width: 400px
  line-height: 18px
  font-size: 15px
  margin-top: 80px
  
  span
    display: block
    margin: 50px
    width: 300px
    font-size: 11px
    line-height: 14px

.bookmarklet
  @include translate( -50% , -50% )
  cursor: -webkit-grab
  cursor: grab
  margin: -3px 0 0 -3px
  box-shadow: 5px 5px 0 0 white
  font-size: 21px
  text-decoration: none
  background-color: red
  padding: 15px
  
  &:active
    cursor: -webkit-grabbing
    cursor: grabbing
            
          
!
            
              # here's the source for anyone that's interested
class Glitch

	# call this to run the script,
	# `window.glitch.run()`
	run: ->
		
		# find all the images
		@.getImages()

		# make a canvas to edit them
		@.buildCanvas()

		# actually glitch the images
		@.process()

		# remove extra dom elements once finished
		@.clean()

	getImages: ->

		# store all image elements to this class
		@.images = document.getElementsByTagName "img"

	buildCanvas: ->

		# make the canvas element
		@.canvas = document.createElement "canvas"

		# get the context for printing image data
		@.stage = @.canvas.getContext "2d"

		# add the canvas to the dom
		document.body.appendChild @.canvas

		# set its inital style
		@.resizeCanvas 1 , 1

	resizeCanvas: ( w , h ) ->

		# set the style attributes for it
		@.canvas.setAttribute "style" , "width: #{w}px; height: #{h}px; position: fixed; opacity: 0; left: -#{w}px; top: -#{h}px;"

		# also set the element attributes
		@.canvas.setAttribute "width" , w
		@.canvas.setAttribute "height" , h

	process: ->

		# loop through each image
		for image in @.images

			# wrap this in a try/catch so security issues don't break it
			try

				# store the image so we can edit it
				@.image = image

				# security hack?
				@.allow()

				# rescale the canvas
				@.setScale()

				# put the image on the canvas so we can get its image data
				@.getImageData()

				# glitch the data uri
				@.glitchData()

				# put the image data back into the image element
				@.setData()

			catch error

				# log the error for those interested
				console.log error

	allow: ->

		@.image.setAttribute "crossOrigin" , "anonymous"

	setScale: ->

		# get the image size
		w = @.image.offsetWidth
		h = @.image.offsetHeight

		# scale the canvas
		@.resizeCanvas w , h

	getImageData: ->

		# draw the image onto the canvas
		@.stage.drawImage @.image , 0 , 0

		# get the image data
		@.data = @.canvas.toDataURL "image/jpeg"

	glitchData: ->

		# get the part of the string that can be glitched and the prefix
		prefix = @.data.substring( 0 , @.data.indexOf( "," ) + 1 )
		image = @.data.substring( @.data.indexOf(",") + 1 )

		# run for a random number of iterations
		i = 0
		p = 1 + Math.floor( Math.random() * 10 )
		while i < p

			# find the point we split at
			point = Math.round( Math.random() * image.length - 1 )

			# rejoin and save it
			image = image.substr( 0 , point ) + image.charAt( point + 1 ) + image.charAt( point ) + image.substr( point + 2 )

			i++

		# store the data again
		@.data = prefix + image

	setData: ->

		# make a new element to place it into
		img = document.createElement "img"

		# get the original scale
		img.setAttribute "width" , @.image.offsetWidth
		img.setAttribute "height" , @.image.offsetHeight

		# apply the source
		img.setAttribute "src" , @.data

		# append the new image and remove the old
		document.body.insertBefore img , @.image
		@.image.parentNode.removeChild @.image

	clean: ->

		# destroy the canvas element
		@.canvas.parentNode.removeChild @.canvas

		# stop referencing the last image
		@.image = null

		# clear image data from class
		@.data = null

window.glitch = new Glitch
glitch.run()
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console