Debian Codegraph @

dojox/mobile/_maskUtils.js

raw symbols SCIP
1 define([
2 "dojo/_base/window",
3 "dojo/dom-style",
4 "./sniff"
5 ], function(win, domStyle, has){
6
7 has.add("mask-image-css", function(global, doc, elt){
8 return typeof doc.getCSSCanvasContext === "function" && typeof elt.style.webkitMaskImage !== "undefined";
9 });
10
11 // Indicates whether image mask is available (either via css mask image or svg)
12 has.add("mask-image", function(){
13 return has("mask-image-css") || has("svg");
14 });
15
16 var cache = {};
17
18 return {
19 // summary:
20 // Utility methods to clip rounded corners of various elements (Switch, ScrollablePane, scrollbars in scrollable widgets).
21 // Uses -webkit-mask-image on webkit, or SVG on other browsers.
22
23 createRoundMask: function(/*DomNode*/node, x, y, r, b, w, h, rx, ry, e){
24 // summary:
25 // Creates and sets a mask for the specified node.
26
27 var tw = x + w + r;
28 var th = y + h + b;
29
30 if(has("mask-image-css")){ // use -webkit-mask-image
31 var id = ("DojoMobileMask" + x + y + w + h + rx + ry).replace(/\./g, "_");
32 if (!cache[id]) {
33 cache[id] = 1;
34 var ctx = win.doc.getCSSCanvasContext("2d", id, tw, th);
35 ctx.beginPath();
36 if (rx == ry) {
37 // round arc
38 if(rx == 2 && w == 5){
39 // optimized case for vertical scrollbar
40 ctx.fillStyle = "rgba(0,0,0,0.5)";
41 ctx.fillRect(1, 0, 3, 2);
42 ctx.fillRect(0, 1, 5, 1);
43 ctx.fillRect(0, h - 2, 5, 1);
44 ctx.fillRect(1, h - 1, 3, 2);
45 ctx.fillStyle = "rgb(0,0,0)";
46 ctx.fillRect(0, 2, 5, h - 4);
47 }else if(rx == 2 && h == 5){
48 // optimized case for horizontal scrollbar
49 ctx.fillStyle = "rgba(0,0,0,0.5)";
50 ctx.fillRect(0, 1, 2, 3);
51 ctx.fillRect(1, 0, 1, 5);
52 ctx.fillRect(w - 2, 0, 1, 5);
53 ctx.fillRect(w - 1, 1, 2, 3);
54 ctx.fillStyle = "rgb(0,0,0)";
55 ctx.fillRect(2, 0, w - 4, 5);
56 }else{
57 // general case
58 ctx.fillStyle = "#000000";
59 ctx.moveTo(x+rx, y);
60 ctx.arcTo(x, y, x, y+rx, rx);
61 ctx.lineTo(x, y+h - rx);
62 ctx.arcTo(x, y+h, x+rx, y+h, rx);
63 ctx.lineTo(x+w - rx, y+h);
64 ctx.arcTo(x+w, y+h, x+w, y+rx, rx);
65 ctx.lineTo(x+w, y+rx);
66 ctx.arcTo(x+w, y, x+w - rx, y, rx);
67 }
68 } else {
69 // elliptical arc
70 var pi = Math.PI;
71 ctx.scale(1, ry / rx);
72 ctx.moveTo(x+rx, y);
73 ctx.arc(x+rx, y+rx, rx, 1.5 * pi, 0.5 * pi, true);
74 ctx.lineTo(x+w - rx, y+2 * rx);
75 ctx.arc(x+w - rx, y+rx, rx, 0.5 * pi, 1.5 * pi, true);
76 }
77 ctx.closePath();
78 ctx.fill();
79 }
80 node.style.webkitMaskImage = "-webkit-canvas(" + id + ")";
81 }else if(has("svg")){ // add an SVG image to clip the corners.
82 if(node._svgMask){
83 node.removeChild(node._svgMask);
84 }
85 var bg = null;
86 for(var p = node.parentNode; p; p = p.parentNode){
87 bg = domStyle.getComputedStyle(p).backgroundColor;
88 if(bg && bg != "transparent" && !bg.match(/rgba\(.*,\s*0\s*\)/)){
89 break;
90 }
91 }
92 var svgNS = "http://www.w3.org/2000/svg";
93 var svg = win.doc.createElementNS(svgNS, "svg");
94 svg.setAttribute("width", tw);
95 svg.setAttribute("height", th);
96 svg.style.position = "absolute";
97 svg.style.pointerEvents = "none";
98 svg.style.opacity = "1";
99 svg.style.zIndex = "2147483647"; // max int
100 var path = win.doc.createElementNS(svgNS, "path");
101 e = e || 0;
102 rx += e;
103 ry += e;
104 // TODO: optimized cases for scrollbars as in webkit case?
105 var d = " M" + (x + rx - e) + "," + (y - e) + " a" + rx + "," + ry + " 0 0,0 " + (-rx) + "," + ry + " v" + (-ry) + " h" + rx + " Z" +
106 " M" + (x - e) + "," + (y + h - ry + e) + " a" + rx + "," + ry + " 0 0,0 " + rx + "," + ry + " h" + (-rx) + " v" + (-ry) + " z" +
107 " M" + (x + w - rx + e) + "," + (y + h + e) + " a" + rx + "," + ry + " 0 0,0 " + rx + "," + (-ry) + " v" + ry + " h" + (-rx) + " z" +
108 " M" + (x + w + e) + "," + (y + ry - e) + " a" + rx + "," + ry + " 0 0,0 " + (-rx) + "," + (-ry) + " h" + rx + " v" + ry + " z";
109 if(y > 0){
110 d += " M0,0 h" + tw + " v" + y + " h" + (-tw) + " z";
111 }
112 if(b > 0){
113 d += " M0," + (y + h) + " h" + tw + " v" + b + " h" + (-tw) + " z";
114 }
115 path.setAttribute("d", d);
116 path.setAttribute("fill", bg);
117 path.setAttribute("stroke", bg);
118 path.style.opacity = "1";
119 svg.appendChild(path);
120 node._svgMask = svg;
121 node.appendChild(svg);
122 }
123 }
124 };
125 });