index.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import {
  2. init,
  3. attributesModule,
  4. styleModule,
  5. eventListenersModule,
  6. h,
  7. } from "../../build/index.js";
  8. const patch = init([attributesModule, styleModule, eventListenersModule]);
  9. let vnode;
  10. let data = {
  11. degRotation: 0,
  12. };
  13. function gRotation() {
  14. // console.log("gRotation: %s", data.degRotation);
  15. return "rotate(" + data.degRotation + "deg)";
  16. }
  17. function triangleClick(id) {
  18. console.log("triangleClick: %s", id);
  19. render();
  20. }
  21. function handleRotate(degs) {
  22. data.degRotation += degs;
  23. console.log("handleRotate: %s, %s", degs, data.degRotation);
  24. render();
  25. }
  26. function handleReset(degs) {
  27. data.degRotation = degs;
  28. console.log("handleReset: %s", degs);
  29. render();
  30. }
  31. function render() {
  32. vnode = patch(vnode, view(data));
  33. }
  34. const hTriangle = (id, degRotation) =>
  35. h("polygon#" + id, {
  36. attrs: {
  37. points: "-50,-88 0,-175 50,-88",
  38. transform: "rotate(" + degRotation + ")",
  39. "stroke-width": 3,
  40. },
  41. on: {
  42. click: () => {
  43. triangleClick(id);
  44. },
  45. },
  46. });
  47. const view = () =>
  48. h("div.view", [
  49. h("h1", "Snabbdom SVG Carousel"),
  50. h(
  51. "svg",
  52. { attrs: { width: 380, height: 380, viewBox: [-190, -190, 380, 380] } },
  53. [
  54. h(
  55. "g#carousel",
  56. {
  57. style: { "-webkit-transform": gRotation(), transform: gRotation() },
  58. },
  59. [
  60. hTriangle("yellow", 0),
  61. hTriangle("green", 60),
  62. hTriangle("magenta", 120),
  63. hTriangle("red", 180),
  64. hTriangle("cyan", 240),
  65. hTriangle("blue", 300),
  66. ]
  67. ),
  68. ]
  69. ),
  70. h(
  71. "button",
  72. {
  73. on: {
  74. click: () => {
  75. handleRotate(60);
  76. },
  77. },
  78. },
  79. "Rotate Clockwise"
  80. ),
  81. h(
  82. "button",
  83. {
  84. on: {
  85. click: () => {
  86. handleRotate(-60);
  87. },
  88. },
  89. },
  90. "Rotate Anticlockwise"
  91. ),
  92. h(
  93. "button",
  94. {
  95. on: {
  96. click: () => {
  97. handleReset(0);
  98. },
  99. },
  100. },
  101. "Reset"
  102. ),
  103. ]);
  104. window.addEventListener("DOMContentLoaded", () => {
  105. const container = document.getElementById("container");
  106. vnode = patch(container, view(data));
  107. render();
  108. });