MCPcopy
hub / github.com/methodofaction/Method-Draw / PaintBox

Function PaintBox

src/js/method-draw.js:1088–1225  ·  view source on GitHub ↗
(container, type)

Source from the content-addressed store, hash-verified

1086 };
1087
1088 var PaintBox = function(container, type) {
1089 var background = document.getElementById("canvas_background");
1090 var cur = {color: "fff", opacity: 1}
1091 if (type == "stroke") cur = curConfig['initStroke'];
1092 if (type == "fill") cur = curConfig['initFill'];
1093 if (type == "canvas" && background) {
1094 var rgb = background.getAttribute("fill").match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
1095 if (rgb) {
1096 var hex = ("0" + parseInt(rgb[1],10).toString(16)).slice(-2) +
1097 ("0" + parseInt(rgb[2],10).toString(16)).slice(-2) +
1098 ("0" + parseInt(rgb[3],10).toString(16)).slice(-2);
1099 cur = {color: hex, opacity: 1}
1100 }
1101 }
1102
1103 // set up gradients to be used for the buttons
1104 var svgdocbox = new DOMParser().parseFromString(
1105 '<svg xmlns="http://www.w3.org/2000/svg"><rect width="100%" height="100%"\
1106 fill="#' + cur.color + '" opacity="' + cur.opacity + '"/>\
1107 <defs><linearGradient id="gradbox_"/></defs></svg>', 'text/xml');
1108 var docElem = svgdocbox.documentElement;
1109
1110 docElem = $(container)[0].appendChild(document.importNode(docElem, true));
1111 if (type === 'canvas') docElem.setAttribute('width',60.5);
1112 else docElem.setAttribute('width',"100%");
1113
1114 this.rect = docElem.firstChild;
1115 this.defs = docElem.getElementsByTagName('defs')[0];
1116 this.grad = this.defs.firstChild;
1117 this.paint = new $.jGraduate.Paint({solidColor: cur.color});
1118 this.type = type;
1119
1120 this.setPaint = function(paint, apply, noUndo) {
1121 this.paint = paint;
1122 var fillAttr = "none";
1123 var ptype = paint.type;
1124 var opac = paint.alpha / 100;
1125 switch ( ptype ) {
1126 case 'solidColor':
1127 fillAttr = (paint[ptype] == 'none' || paint[ptype] == 'one') ? 'none' : "#" + paint[ptype];
1128 break;
1129 case 'linearGradient':
1130 case 'radialGradient':
1131 this.defs.removeChild(this.grad);
1132 this.grad = this.defs.appendChild(paint[ptype]);
1133 var id = this.grad.id = 'gradbox_' + this.type;
1134 fillAttr = "url(#" + id + ')';
1135 }
1136 this.rect.setAttribute('fill', fillAttr);
1137 this.rect.setAttribute('opacity', opac);
1138
1139 if (this.type == "canvas") {
1140 //recache background in case it changed
1141 var background = document.getElementById("canvas_background");
1142 if (background) {
1143 res = svgCanvas.getResolution()
1144 background.setAttribute("x", -1);
1145 background.setAttribute("y", -1);

Callers

nothing calls this directly

Calls 3

$Function · 0.85
createBackgroundFunction · 0.85
getPaintFunction · 0.70

Tested by

no test coverage detected