Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Complete merging PR #221 "Render to canvas" and add test use case.
Fixes #67
- Loading branch information
1 parent
4340ff1
commit 717b928
Showing
7 changed files
with
141 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,59 @@ | ||
import { getEnabledElements } from '../enabledElements.js'; | ||
import displayImage from '../displayImage.js'; | ||
import enableElement from '../enable.js'; | ||
import triggerEvent from '../triggerEvent.js'; | ||
import EVENTS from '../events.js'; | ||
import drawImageSync from '../internal/drawImageSync.js'; | ||
import getDefaultViewport from '../internal/getDefaultViewport.js'; | ||
import webGL from '../webgl/index.js'; | ||
|
||
export default function (canvas, imageID, viewport) { | ||
|
||
export default function (canvas, image, viewport = null, options = null) { | ||
if (canvas === undefined) { | ||
throw new Error('renderToCanvas: parameter canvas cannot be undefined'); | ||
} | ||
|
||
const enabledElements = getEnabledElements(); | ||
const elements = []; | ||
|
||
enabledElements.forEach(function (enabledElement) { | ||
if (enabledElement.canvas && enabledElement.canvas === canvas) { | ||
elements.push(enabledElement.element); | ||
// If this enabled element has the option set for WebGL, we should | ||
// Check if this device actually supports it | ||
if (options && options.renderer && options.renderer.toLowerCase() === 'webgl') { | ||
if (webGL.renderer.isWebGLAvailable()) { | ||
// If WebGL is available on the device, initialize the renderer | ||
// And return the renderCanvas from the WebGL rendering path | ||
webGL.renderer.initRenderer(); | ||
options.renderer = 'webgl'; | ||
} else { | ||
// If WebGL is not available on this device, we will fall back | ||
// To using the Canvas renderer | ||
console.error('WebGL not available, falling back to Canvas renderer'); | ||
delete options.renderer; | ||
} | ||
}); | ||
} | ||
|
||
const enabledElementStub = { | ||
element: canvas, | ||
canvas, | ||
image, | ||
invalid: true, // True if image needs to be drawn, false if not | ||
needsRedraw: true, | ||
options: null, | ||
layers: [], | ||
data: {}, | ||
renderingTools: {}, | ||
viewport: getDefaultViewport(canvas, image) | ||
}; | ||
|
||
if (elements.length === 0) { | ||
enableElement(canvas, null, canvas); | ||
elements.push(canvas); | ||
// Merge viewport | ||
if (viewport) { | ||
for (const attrname in viewport) { | ||
if (viewport[attrname] !== null) { | ||
enabledElementStub.viewport[attrname] = viewport[attrname]; | ||
} | ||
} | ||
} | ||
|
||
elements.forEach(function (element) { | ||
displayImage(element, imageID, viewport); | ||
}); | ||
const eventDetails = { | ||
enabledElement: enabledElementStub, | ||
timestamp: Date.now() | ||
}; | ||
|
||
triggerEvent(enabledElementStub.element, EVENTS.PRE_RENDER, eventDetails); | ||
|
||
drawImageSync(enabledElementStub, enabledElementStub.invalid); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export default '2.2.0'; | ||
export default '2.2.1-rc1'; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { assert, expect } from 'chai'; | ||
|
||
import renderToCanvas from '../../src/rendering/renderToCanvas.js'; | ||
import { getEnabledElement } from '../../src/enabledElements.js'; | ||
|
||
describe('renderToCanvas', function () { | ||
beforeEach(function () { | ||
// Arrange | ||
this.canvas1 = document.createElement('canvas'); | ||
|
||
this.height = 256; | ||
this.width = 256; | ||
|
||
const canvasContext = this.canvas1.getContext('2d'); | ||
const imageData = canvasContext.createImageData(this.width, this.height); | ||
const rnd = Math.round(Math.random() * 255); | ||
const pixelData = imageData.data; | ||
let index = 0; | ||
|
||
for (let y = 0; y < this.height; y++) { | ||
for (let x = 0; x < this.width; x++) { | ||
pixelData[index++] = (x + rnd) % 256; // RED | ||
pixelData[index++] = 0; // GREEN | ||
pixelData[index++] = 0; // BLUE | ||
pixelData[index++] = 255; // ALPHA | ||
} | ||
} | ||
canvasContext.putImageData(imageData, 0, 0); | ||
|
||
function getPixelData () { | ||
return pixelData; | ||
} | ||
|
||
this.image = { | ||
imageId: 'exampleImageId', | ||
minPixelValue: 0, | ||
maxPixelValue: 255, | ||
slope: 1.0, | ||
intercept: 0, | ||
windowCenter: 127, | ||
windowWidth: 256, | ||
getPixelData, | ||
rows: this.height, | ||
columns: this.width, | ||
height: this.height, | ||
width: this.width, | ||
color: true, | ||
sizeInBytes: this.width * this.height * 2 | ||
}; | ||
}); | ||
|
||
it('should render an image on the existing canvas, then ', function (done) { | ||
// Arrange | ||
const renderCanvas1 = document.createElement('canvas'); | ||
|
||
renderCanvas1.width = this.width; | ||
renderCanvas1.height = this.height; | ||
|
||
renderCanvas1.addEventListener('cornerstoneimagerendered', (event) => { | ||
// Assert | ||
assert.equal(event.target, renderCanvas1); | ||
|
||
expect(function () { | ||
getEnabledElement(event.target); | ||
}).to.throw('element not enabled'); | ||
|
||
done(); | ||
}); | ||
|
||
// Act | ||
renderToCanvas(renderCanvas1, this.image); | ||
|
||
const canvasContext1 = this.canvas1.getContext('2d'); | ||
const imageData1 = canvasContext1.createImageData(this.width, this.height); | ||
const pixelData1 = imageData1.data; | ||
|
||
const canvasContext2 = renderCanvas1.getContext('2d'); | ||
const imageData2 = canvasContext2.createImageData(this.width, this.height); | ||
const pixelData2 = imageData2.data; | ||
|
||
// Assert | ||
assert.deepEqual(pixelData1, pixelData2); | ||
}); | ||
}); |