source: Dev/trunk/src/client/dojox/mobile/_maskUtils.js @ 532

Last change on this file since 532 was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

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