Skip to content

Commit

Permalink
Reuse createImageBitmap polyfill
Browse files Browse the repository at this point in the history
  • Loading branch information
brianpeiris committed May 14, 2019
1 parent 801079c commit 5c0af84
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
24 changes: 1 addition & 23 deletions src/components/camera-tool.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { spawnMediaAround } from "../utils/media-utils";
import { createImageBitmap } from "../utils/image-bitmap-utils";
import { ObjectTypes } from "../object-types";
import { paths } from "../systems/userinput/paths";
import { SOUND_CAMERA_TOOL_TOOK_SNAPSHOT } from "../systems/sound-effects-system";
Expand All @@ -19,29 +20,6 @@ const pathsMap = {
}
};

const createImageBitmap =
window.createImageBitmap ||
async function(data) {
return new Promise(resolve => {
// https://dev.to/nektro/createimagebitmap-polyfill-for-safari-and-edge-228
// https://gist.github.com/MonsieurV/fb640c29084c171b4444184858a91bc7

const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
canvas.width = data.width;
canvas.height = data.height;
ctx.putImageData(data, 0, 0);
const dataURL = canvas.toDataURL();
const img = document.createElement("img");

img.addEventListener("load", function() {
resolve(this);
});

img.src = dataURL;
});
};

const snapCanvas = document.createElement("canvas");
async function pixelsToPNG(pixels, width, height) {
snapCanvas.width = width;
Expand Down
1 change: 1 addition & 0 deletions src/react-components/avatar-preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import classNames from "classnames";
import { createDefaultEnvironmentMap } from "../components/environment-map";
import { loadGLTF } from "../components/gltf-model-plus";
import { disposeNode } from "../utils/three-utils";
import { createImageBitmap } from "../utils/image-bitmap-utils";
import styles from "../assets/stylesheets/avatar-preview.scss";

const TEXTURE_PROPS = {
Expand Down
22 changes: 22 additions & 0 deletions src/utils/image-bitmap-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export const createImageBitmap =
window.createImageBitmap ||
async function(data) {
return new Promise(resolve => {
// https://dev.to/nektro/createimagebitmap-polyfill-for-safari-and-edge-228
// https://gist.github.com/MonsieurV/fb640c29084c171b4444184858a91bc7

const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
canvas.width = data.width;
canvas.height = data.height;
ctx.putImageData(data, 0, 0);
const dataURL = canvas.toDataURL();
const img = document.createElement("img");

img.addEventListener("load", function() {
resolve(this);
});

img.src = dataURL;
});
};

0 comments on commit 5c0af84

Please sign in to comment.