8000 Download canvas image onclick · Issue #1 · wwwtyro/space-2d · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Download canvas image onclick #1
Open
@zorker

Description

@zorker

Not really an issue. But not sure where else I could write this.

I wanted to use your script to create space wallpapers for myself.

Since the canvas context is webgl I needed to preserve the drawing buffer. Otherwise the canvas blob will be blank.

//addition to the canvas settings in bundle.js
var canvas = document.getElementById('render-canvas');
var glContextAttributes = { preserveDrawingBuffer: true };
var gl = canvas.getContext("experimental-webgl", glContextAttributes);
canvas.width = 2560; //window.innerWidth - (300 + 16 + 16 + 16);
canvas.height = 1440; //window.innerHeight - (16 + 16);

I rerender the image by reloading the browser. Maybe you find a better solution that makes the preserveDrawingBuffer work with the gui redraw.

//download img as png on canvas click
  (function() {
    var canvas = document.querySelector("#render-canvas");
    var uri;
    canvas.onclick = function(evt) {
      evt.preventDefault();
      canvas.toBlob(function(blob) {
        if(uri)
          (window.URL||window.webkitURL).revokeObjectURL(uri);
        uri = (window.URL||window.webkitURL).createObjectURL(blob);
        var a = document.createElement('a');
        a.href = uri;
        a.download="stars.png";
        document.body.appendChild(a);
        a.onclick = function(evt) { this.parentNode.removeChild(this); }
        a.click();
      });
    }
  })();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0