question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Browser crash when trying to convert html + svg to scaled image

See original GitHub issue

image

I am using Treant.js javascript library to create an organisational chart. I used the following code to convert the SVG elements (the lines that connect the boxes) and HTML contents (the boxes):

	var useWidth = $( '#orgchart' ).prop( 'scrollWidth' ); 
	var useHeight = $( '#orgchart' ).prop( 'scrollHeight' ); 

	var scaleBy = 5;

	var w = useWidth;
	var h = useHeight;
	var scaledCanvas = document.createElement( 'canvas' );
	scaledCanvas.width = w * scaleBy;
	scaledCanvas.height = h * scaleBy;
	var scaledContext = scaledCanvas.getContext( '2d' );
	scaledContext.scale( scaleBy, scaleBy );

	html2canvas( $( "#orgchart" ), {
		canvas : scaledCanvas,
		logging : false,
		onrendered : function ( canvas ) {

			$( "#orgChartSketchDivBody" ).html( "<canvaS id=\"orgChartSketch\"></canvas>" );

			var sketchCanvas = document.getElementById( 'orgChartSketch' );
			var sketchContext = sketchCanvas.getContext( '2d' );
			sketchCanvas.width = w * scaleBy;
			sketchCanvas.height = h * scaleBy;
			sketchContext.drawImage( canvas, 0, 0 );
			sketchContext.scale( ( w * scaleBy ) / ( h * scaleBy ), ( w * scaleBy ) / ( h * scaleBy ) );

			var imgData = sketchCanvas.toDataURL( 'image/png' );
			}
	} );

This works fine as long as the chart is small. But for large charts, when rendered, the browser freezes and crashes (I am using latest Chrome). I know the scale value 5 is high, but I need high quality images so as to get readable images of large charts. Is there any solution to stop the browser from crashing?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
eKoopmanscommented, Jun 14, 2017

Hi @tanzid203, looking at your code I’m a bit confused why you’ve created sketchCanvas. If your goal is to get imgData, you can use .toDataURL() directly on the canvas given by the onrendered function. Also, your final scale could simplify to sketchContext.scale(w/h, w/h), though I’m not sure the purpose of that scale, since it only affects drawing done on it after the scale.

Other than that, I’m not sure about your performance issues, sorry!

0reactions
no-response[bot]commented, Dec 12, 2017

This issue has been automatically closed because there has been no response to our request for more information from the original author. With only the information that is currently in the issue, we don’t have enough information to take action. Please reach out if you have or find the answers we need so that we can investigate further.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Browser crash when trying to convert HTML+ SVG content to ...
I know the scale value 5 is high, but I need high quality images so as to get readable images of large charts....
Read more >
Browser Crash When Trying To Convert Html+ Svg ... - ADocLib
Scalable Vector Graphics or SVG, is a markup language for creating vector based the HTML, this means the image can still be seen...
Read more >
some SVG images hang browser when loaded via data URI
We've found that some images, at least some SVG files, cause Firefox 7.0.1, 8beta, and 9/aurora to hang during this process, while Firefox...
Read more >
Raster images inside inline SVG are scaled extremely blurrily
I'm not certain if this is the same issue but we're trying to use SVG's to do image masking. In our case, we...
Read more >
A Complete Guide to SVG Fallbacks - CSS-Tricks
To get an image fallback in SVG, without extra downloads, you need a way of including an image: That old browsers will recognize...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found