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.

Way of saving canvas to image in deck.gl

See original GitHub issue

Hi, I want to save the webgl canvas to image by using toDataURL or gl.readPixels and have some questions:

  • In order to read from canvas, seems I need to use deck.redraw() to trigger the render and read from framebuffer. Since I am using <DeckGL> in React and, according to the code the this.deck is not exposed from <DeckGL>, how could I achieve this without passing customized deck instance? Use ref.current.deck.redraw(true)
  • What is your recommendation of the way saving canvas to image, in deck.gl? Thanks!

Issue Analytics

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

github_iconTop GitHub Comments

3reactions
Pessimistresscommented, Apr 7, 2020

The general idea:

let screenshotCb = null;
const myDeckInstance = new deck.Deck({
  ...
  glOptions: {
    preserveDrawingBuffer: true  // https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/getContext
  },
  onAfterRender: () => {
    if (screenshotCb) {
      screenshotCb(myDeckInstance.canvas.toDataURL());
      screenshotCb = null;
    }
  }
});
2reactions
tsherifcommented, Apr 7, 2020

Great! It doesn’t look like you need that html2canvas tool though. Deck.gl maintains a reference to its canvas. I believe the rest of your code will work the same way, i.e.

let canvas = $scope.scatterLayer.canvas;
document.body.appendChild(canvas);
let a = document.createElement('a');
// toDataURL defaults to png, so we need to request a jpeg, then convert for file download.
a.href = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream");
a.download = 'screenshot.png';
a.click();
Read more comments on GitHub >

github_iconTop Results From Across the Web

Way of saving canvas to image in deck.gl · Issue #4436 - GitHub
Hi, I want to save the webgl canvas to image by using toDataURL or gl.readPixels and have some questions: In order to read...
Read more >
deck.gl Capture Canvas as Image - CodePen
deck.gl Capture Canvas as Image · Miss X Follow. Love Run. Pen Editor Menu. Settings. Change View. Use Left Layout Use Top Layout...
Read more >
Views and Projections - deck.gl
The position and extent of the view on the canvas: x , y , width , and height ... 10); // Save the...
Read more >
mapbox gl js - Export png from deckl map - Stack Overflow
1 Answer 1 · Aadd id to Deckgl Component and ref return ( <DeckGL id="deck-gl-canvas" ref={this.deckRef} layers={layers} onHover={this. · add ref ...
Read more >
Animated Map Visualizations with Deck.gl - YouTube
https://twitter.com/pbesh, @cortico, MIT @medialab.Visualize and animate data with interactive maps powered by http:// deck. gl - Uber's ...
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