3 <script type=
"text/javascript">
4 function assert(cond
, msg
) { if (!cond
) { throw msg
; } }
5 window
.onload = function() {
7 var ctx
= document
.getElementById("c1").getContext("2d");
9 assert(0 === ctx
.getLineDash().length
,
10 "Default dash is [ ] (none)");
11 assert(0 === ctx
.lineDashOffset
,
12 "Default dashOffset is 0 (none)");
14 ctx
.setLineDash([ 2 ]);
15 assert(2 === ctx
.getLineDash().length
&&
16 2 === ctx
.getLineDash()[0] &&
17 2 === ctx
.getLineDash()[1],
18 "dash = [ 2 ] works");
19 ctx
.setLineDash([ 2 ]);
21 assert(0 === ctx
.getLineDash().length
,
23 ctx
.setLineDash([ 2 ]);
24 ctx
.setLineDash([ 0, 0, 0 ]);
25 assert(6 === ctx
.getLineDash().length
,
26 0 === ctx
.getLineDash()[0] &&
27 0 === ctx
.getLineDash()[1] &&
28 0 === ctx
.getLineDash()[2] &&
29 0 === ctx
.getLineDash()[3] &&
30 0 === ctx
.getLineDash()[4] &&
31 0 === ctx
.getLineDash()[5],
32 "dash = [ 0, 0, 0 ] works");
34 ctx
.setLineDash([ 2 ]);
35 assert(0 === ctx
.lineDashOffset
, "dashOffset is 0");
36 ctx
.lineDashOffset
= 1;
37 assert(1 === ctx
.lineDashOffset
, "Setting dashOffset succeeded");
39 assert(1 === ctx
.lineDashOffset
, "Changing dash does not reset dashOffset");
41 // NB: might want to add a |.dash = number| special case,
42 // don't test that it fails here. Might also want to add a
43 // |.dash = [0]| special case for resetting, so don't test
53 ctx
.setLineDash([ 2 ]);
54 for (var i
= 0; i
< badVals
.length
; ++i
) {
56 try { ctx
.setLineDash(badVals
[i
]); }
57 catch(e
) { error
= true; }
59 2 === ctx
.getLineDash().length
&&
60 2 === ctx
.getLineDash()[0] &&
61 2 === ctx
.getLineDash()[1],
62 "Expected setLineDash("+ badVals
[i
] +") to throw exception and not change dash");
66 [ "array of string" ],
70 ctx
.setLineDash([ 2 ]);
71 for (var i
= 0; i
< ignoredVals
.length
; ++i
) {
72 ctx
.setLineDash(ignoredVals
[i
]);
73 assert(2 === ctx
.getLineDash().length
&&
74 2 === ctx
.getLineDash()[0] &&
75 2 === ctx
.getLineDash()[1],
76 "Expected |setLineDash(" + ignoredVals
[i
] + ") to not change dash");
79 ctx
.setLineDash([ 2 ]);
81 ctx
.setLineDash([ 1, 1, 1, 1 ]);
83 assert(2 === ctx
.getLineDash().length
&&
84 2 === ctx
.getLineDash()[0] &&
85 2 === ctx
.getLineDash()[1],
86 "dash was saved then restored");
88 document
.body
.innerHTML
= "FAIL: "+ e
.toString();
91 document
.body
.innerHTML
= "Pass";
96 <div><canvas id=
"c1" width=
"300" height=
"300"></canvas></div>