mirror of
https://github.com/openstf/stf
synced 2025-10-05 10:39:25 +02:00
43 lines
1.3 KiB
JavaScript
43 lines
1.3 KiB
JavaScript
function CanvasRender(canvasElement, options) {
|
|
this.options = options
|
|
this.context = canvasElement.getContext('2d')
|
|
|
|
var devicePixelRatio = window.devicePixelRatio || 1
|
|
|
|
var backingStoreRatio = this.context.webkitBackingStorePixelRatio ||
|
|
this.context.mozBackingStorePixelRatio ||
|
|
this.context.msBackingStorePixelRatio ||
|
|
this.context.oBackingStorePixelRatio ||
|
|
this.context.backingStorePixelRatio || 1
|
|
|
|
this.frontBackRatio = devicePixelRatio / backingStoreRatio
|
|
|
|
if (options.autoScaleForRetina && devicePixelRatio !== backingStoreRatio) {
|
|
var oldWidth = canvasElement.width
|
|
var oldHeight = canvasElement.height
|
|
|
|
canvasElement.width = oldWidth * this.frontBackRatio
|
|
canvasElement.height = oldHeight * this.frontBackRatio
|
|
|
|
canvasElement.style.width = oldWidth + 'px'
|
|
canvasElement.style.height = oldHeight + 'px'
|
|
|
|
this.context.scale(this.frontBackRatio, this.frontBackRatio)
|
|
}
|
|
}
|
|
|
|
CanvasRender.prototype.draw = function(image) {
|
|
this.context.drawImage(image, 0, 0)
|
|
}
|
|
|
|
CanvasRender.prototype.clear = function() {
|
|
this.context.clearRect(0, 0, this.context.canvas.width,
|
|
this.context.canvas.height)
|
|
}
|
|
|
|
// Check for Non CommonJS world
|
|
if (typeof module !== 'undefined') {
|
|
module.exports = {
|
|
CanvasRender: CanvasRender
|
|
}
|
|
}
|