1 // TODO different stroke behavior for area segment?
  2 // TODO when segmented changes, need insertBefore not appendChild
  3 
  4 pv.SvgScene.area = function(scenes) {
  5 
  6   /*
  7    * Rather than using the default group element, since we know areas only
  8    * contain a single polygon element, use that instead. However, since we won't
  9    * be appending children to the group element, instead assume it will be
 10    * invisible by default.
 11    */
 12   var area = this.cache(scenes, "polygon", "area"), g = scenes.scene.g;
 13   area.setAttribute("display", "none");
 14   if (g) g.setAttribute("display", "none");
 15 
 16   /* segmented */
 17   if (!scenes.length) return;
 18   var s = scenes[0];
 19   if (s.segmented) {
 20     this.areaSegment(scenes);
 21     return;
 22   }
 23 
 24   /* visible */
 25   if (!s.visible) return;
 26   var fill = pv.color(s.fillStyle), stroke = pv.color(s.strokeStyle);
 27   if (!fill.opacity && !stroke.opacity) return;
 28 
 29   /* points */
 30   var p1 = "", p2 = "";
 31   for (var i = 0, j = scenes.length - 1; j >= 0; i++, j--) {
 32     var si = scenes[i], sj = scenes[j];
 33     p1 += si.left + "," + si.top + " ";
 34     p2 += (sj.left + sj.width) + "," + (sj.top + sj.height) + " ";
 35 
 36     /* interpolate (assume linear by default) */
 37     if (i < scenes.length - 1) {
 38       var sk = scenes[i + 1], sl = scenes[j - 1];
 39       switch (s.interpolate) {
 40         case "step-before": {
 41           p1 += si.left + "," + sk.top + " ";
 42           p2 += (sl.left + sl.width) + "," + (sj.top + sj.height) + " ";
 43           break;
 44         }
 45         case "step-after": {
 46           p1 += sk.left + "," + si.top + " ";
 47           p2 += (sj.left + sj.width) + "," + (sl.top + sl.height) + " ";
 48           break;
 49         }
 50       }
 51     }
 52   }
 53 
 54   area.removeAttribute("display");
 55   area.setAttribute("cursor", s.cursor);
 56   area.setAttribute("points", p1 + p2);
 57   area.setAttribute("fill", fill.color);
 58   area.setAttribute("fill-opacity", fill.opacity);
 59   area.setAttribute("stroke", stroke.color);
 60   area.setAttribute("stroke-opacity", stroke.opacity);
 61   area.setAttribute("stroke-width", s.lineWidth);
 62 
 63   var title = this.title(area, s);
 64   if (!title.parentNode) {
 65     this.listen(area, scenes, 0);
 66     this.parentNode(scenes).appendChild(title);
 67   }
 68 };
 69 
 70 pv.SvgScene.areaSegment = function(scenes) {
 71   var g = this.group(scenes), s = scenes[0];
 72   for (var i = 0, n = scenes.length - 1; i < n; i++) {
 73     var s1 = scenes[i], s2 = scenes[i + 1];
 74 
 75     /* visible */
 76     if (!s1.visible || !s2.visible) continue;
 77     var fill = pv.color(s1.fillStyle), stroke = pv.color(s1.strokeStyle);
 78     if (!fill.opacity && !stroke.opacity) continue;
 79 
 80     /* points */
 81     var p = s1.left + "," + s1.top + " "
 82         + s2.left + "," + s2.top + " "
 83         + (s2.left + s2.width) + "," + (s2.top + s2.height) + " "
 84         + (s1.left + s1.width) + "," + (s1.top + s1.height);
 85 
 86     var segment = this.cache(s1, "polygon", "segment");
 87     segment.setAttribute("cursor", s1.cursor);
 88     segment.setAttribute("points", p);
 89     segment.setAttribute("fill", fill.color);
 90     segment.setAttribute("fill-opacity", fill.opacity);
 91     segment.setAttribute("stroke", stroke.color);
 92     segment.setAttribute("stroke-opacity", stroke.opacity);
 93     segment.setAttribute("stroke-width", s1.lineWidth);
 94     this.listen(segment, scenes, i);
 95     g.appendChild(this.title(segment, s1));
 96   }
 97   g.removeAttribute("display");
 98 };
 99