Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/image/loading_displaying.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,19 @@ function loadingDisplaying(p5, fn){

} else {
// Non-GIF Section
const blob = new Blob([data]);
const img = await createImageBitmap(blob);
const img = await new Promise((resolve, reject) => {
const img = new Image();
const blob = new Blob([data], { type: contentType });
const url = URL.createObjectURL(blob);

img.onerror = e => reject(e);
img.onload = () => {
URL.revokeObjectURL(url);
resolve(img);
};

img.src = url;
});

pImg.width = pImg.canvas.width = img.width;
pImg.height = pImg.canvas.height = img.height;
Expand Down
8 changes: 8 additions & 0 deletions test/unit/assets/green.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions test/unit/image/loading.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ suite('loading images', function() {
const disposeNoneGif = '/test/unit/assets/dispose_none.gif';
const disposeBackgroundGif = '/test/unit/assets/dispose_background.gif';
const disposePreviousGif = '/test/unit/assets/dispose_previous.gif';
const svgImage = '/test/unit/assets/green.svg';
const invalidFile = '404file';

beforeAll(async function() {
Expand Down Expand Up @@ -238,6 +239,21 @@ suite('loading images', function() {
assert.equal(gifImage.gifProperties.displayIndex, 0);
assert.equal(gifImage.gifProperties.timeDisplayed, 0);
});

test('loads vector (SVG) images', async () => {
await expect(mockP5Prototype.loadImage(svgImage)).resolves.not.toThrow();

const img = await mockP5Prototype.loadImage(svgImage);
assert.isTrue(img instanceof mockP5.Image);

// ensure the sample svg is fully parsed and drawn correctly
assert.equal(img.width, 48);
assert.equal(img.height, 48);

const GREEN = [0, 255, 0, 255];
assert.deepEqual(img.get(0, 0), GREEN);
assert.deepEqual(img.get(47, 47), GREEN);
});
});

suite.todo('displaying images', function() {
Expand Down