bezier.html 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Bezier Example</title>
  5. <script src="../dist/kity.js"></script>
  6. <script>
  7. </script>
  8. </head>
  9. <body></body>
  10. <script>
  11. var BezierViewer = kity.createClass({
  12. base: kity.Group,
  13. constructor: function(bezier) {
  14. this.callBase();
  15. this.addShape(bezier);
  16. this.bezier = bezier.fill(new kity.Color('red').set('a', 0.2));
  17. this.update();
  18. },
  19. update: function() {
  20. var points = this.bezier.getBezierPoints();
  21. if (this.group) {
  22. this.group.remove();
  23. }
  24. this.addShape(this.group = new kity.Group().pipe(function() {
  25. for (var i = 0; i < points.length; i++) {
  26. var p = points[i], f, b;
  27. f = p.getForward();
  28. b = p.getBackward();
  29. p = p.getVertex();
  30. this.addShape(new kity.Path().pipe(function() {
  31. var d = this.getDrawer();
  32. d.moveTo(b.x, b.y);
  33. d.lineTo(p.x, p.y);
  34. d.lineTo(f.x, f.y);
  35. this.fill('none');
  36. this.stroke('red');
  37. }));
  38. this.addShape(new kity.Rect(6, 6, b.x - 3, b.y - 3).stroke('red').fill('white'));
  39. this.addShape(new kity.Rect(6, 6, f.x - 3, f.y - 3).stroke('red').fill('white'));
  40. this.addShape(new kity.Rect(6, 6, p.x - 3, p.y - 3).fill('red'));
  41. }
  42. }));
  43. }
  44. });
  45. var paper = new kity.Paper(document.body);
  46. paper.setWidth(600).setHeight(400);
  47. var bezier = new kity.Bezier().addPoints([
  48. new kity.BezierPoint(300, 150, false).setForward(350, 50).setBackward(250, 50),
  49. new kity.BezierPoint(450, 150).setForward(470, 200),
  50. new kity.BezierPoint(300, 350, false).setForward(200, 300).setBackward(400, 300),
  51. new kity.BezierPoint(150, 150).setBackward(130, 200),
  52. new kity.BezierPoint(300, 150, false).setForward(350, 50).setBackward(250, 50)
  53. ]);
  54. paper.addShape(window.v = new BezierViewer(bezier));
  55. window.p = bezier.getPoint(1);
  56. </script>
  57. </html>