graphonline/script/features/draw_graph/model/BaseBackgroundDrawer.js
Oleg Sh 43a4b44a22 Change script location.
Split js code.
Added cache and changed loading mechanism for js sources.
2023-11-06 19:16:50 +02:00

89 lines
2.3 KiB
JavaScript

/**
* Graph drawer.
*/
function CommonBackgroundStyle()
{
this.commonColor = '#ffffff';
this.commonOpacity = 1.0;
this.image = null;
}
CommonBackgroundStyle.prototype.Clear = function ()
{
delete this.commonColor;
delete this.commonOpacity;
delete this.image;
}
CommonBackgroundStyle.prototype.ShouldLoad = function (field)
{
return true;
}
CommonBackgroundStyle.prototype.saveToJson = function (field)
{
return JSON.stringify({commonColor: this.commonColor, commonOpacity: this.commonOpacity, image: this.image != null ? this.image.src : null});
}
CommonBackgroundStyle.prototype.loadFromJson = function (json, callbackOnLoaded)
{
this.commonColor = json["commonColor"];
this.commonOpacity = json["commonOpacity"];
this.image = null;
if (typeof json["image"] === 'string') {
this.image = new Image();
this.image.onload = function() {
callbackOnLoaded();
}
this.image.src = json["image"];
}
}
PrintBackgroundStyle.prototype = Object.create(CommonBackgroundStyle.prototype);
function PrintBackgroundStyle()
{
CommonBackgroundStyle.apply(this, arguments);
this.commonColor = '#ffffff';
this.commonOpacity = 1.0;
this.image = null;
}
function BaseBackgroundDrawer(context)
{
this.context = context;
}
BaseBackgroundDrawer.prototype.Draw = function(style, width, height, position, scale)
{
var context = this.context;
var rect = new Rect(position, position.add(new Point(width / scale, height / scale)));
context.clearRect(-rect.minPoint.x, -rect.minPoint.y, rect.size().x + 1, rect.size().y + 1);
var oldOpacity = context.globalAlpha;
if (style.commonOpacity > 0.0)
{
context.globalAlpha = style.commonOpacity;
context.fillStyle = style.commonColor;
context.fillRect(-rect.minPoint.x, -rect.minPoint.y, rect.size().x + 1, rect.size().y + 1);
this.DrawImage(style, width, height, position, scale);
}
context.globalAlpha = oldOpacity;
}
BaseBackgroundDrawer.prototype.DrawImage = function(style, width, height, position, scale)
{
if (style.image == null) {
return;
}
var context = this.context;
context.clearRect(0, 0, style.image.width, style.image.height);
context.drawImage(style.image, 0, 0)
}