Created
December 12, 2016 12:51
-
-
Save negatronGister/47c7d40cb7ae8590f93e304bec0a71b5 to your computer and use it in GitHub Desktop.
Draw canvas from html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| MakeCanvasses = do -> | |
| run = -> | |
| $canvasContainer = $ '#canvas-container', gv.$wrapper | |
| $buttons = gv.$buttonContainer.children '.follow-button-container' | |
| $buttons.each -> | |
| console.log 'make canvas' | |
| console.log $(this) | |
| canvas = createCanvas $(this) | |
| $canvasContainer.append canvas.$el | |
| createCanvas = ( $button ) -> | |
| $iframe = $button.children().first() | |
| buttonWidth = $iframe.outerWidth() | |
| buttonHeight = $iframe.outerHeight() | |
| console.log 'buttonWidth: '+buttonWidth | |
| canvas = setupCanvas({ | |
| w: buttonWidth | |
| h: buttonHeight | |
| }) | |
| buttonHtml = $iframe.clone().wrap('<div>').parent().html() | |
| svgData = convertToSvg buttonHtml, buttonWidth, buttonHeight | |
| console.log buttonHtml | |
| DOMURL = window.URL || window.webkitURL || window | |
| svg = new Blob( [ svgData ], {type: 'image/svg+xml'} ) | |
| imgSrc = DOMURL.createObjectURL( svg ) | |
| drawOnCanvas canvas.ctx, imgSrc, DOMURL | |
| return canvas | |
| convertToSvg = ( markup, width = 200, height = 200 ) -> | |
| return """ | |
| <svg xmlns="http://www.w3.org/2000/svg" width="#{ width }" height="#{ height }"> | |
| <foreignObject width="100%" height="100%"> | |
| <div xmlns="http://www.w3.org/1999/xhtml"> | |
| #{ markup } | |
| </div> | |
| </foreignObject> | |
| </svg> | |
| """ | |
| setupCanvas = ( size ) -> | |
| canvas = document.createElement 'canvas' | |
| ctx = canvas.getContext('2d') | |
| $el = $(canvas) | |
| if window.devicePixelRatio | |
| $el.css | |
| width: size.w | |
| height: size.h | |
| size.w *= window.devicePixelRatio | |
| size.h *= window.devicePixelRatio | |
| canvas.width = size.w | |
| canvas.height = size.h | |
| return { | |
| el: canvas | |
| $el: $el | |
| ctx: ctx | |
| } | |
| drawOnCanvas = ( ctx, imgSrc, DOMURL ) -> | |
| img = new Image() | |
| img.onload = -> | |
| ctx.drawImage img, 0, 0 | |
| DOMURL.revokeObjectURL( imgSrc ) | |
| img.src = imgSrc | |
| { | |
| run: run | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment