SVGVisualElement.mjs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { frame } from 'motion-dom';
  2. import { createBox } from '../../projection/geometry/models.mjs';
  3. import { DOMVisualElement } from '../dom/DOMVisualElement.mjs';
  4. import { camelToDash } from '../dom/utils/camel-to-dash.mjs';
  5. import { getDefaultValueType } from '../dom/value-types/defaults.mjs';
  6. import { transformProps } from '../html/utils/keys-transform.mjs';
  7. import { buildSVGAttrs } from './utils/build-attrs.mjs';
  8. import { camelCaseAttributes } from './utils/camel-case-attrs.mjs';
  9. import { isSVGTag } from './utils/is-svg-tag.mjs';
  10. import { updateSVGDimensions } from './utils/measure.mjs';
  11. import { renderSVG } from './utils/render.mjs';
  12. import { scrapeMotionValuesFromProps } from './utils/scrape-motion-values.mjs';
  13. class SVGVisualElement extends DOMVisualElement {
  14. constructor() {
  15. super(...arguments);
  16. this.type = "svg";
  17. this.isSVGTag = false;
  18. this.measureInstanceViewportBox = createBox;
  19. this.updateDimensions = () => {
  20. if (this.current && !this.renderState.dimensions) {
  21. updateSVGDimensions(this.current, this.renderState);
  22. }
  23. };
  24. }
  25. getBaseTargetFromProps(props, key) {
  26. return props[key];
  27. }
  28. readValueFromInstance(instance, key) {
  29. if (transformProps.has(key)) {
  30. const defaultType = getDefaultValueType(key);
  31. return defaultType ? defaultType.default || 0 : 0;
  32. }
  33. key = !camelCaseAttributes.has(key) ? camelToDash(key) : key;
  34. return instance.getAttribute(key);
  35. }
  36. scrapeMotionValuesFromProps(props, prevProps, visualElement) {
  37. return scrapeMotionValuesFromProps(props, prevProps, visualElement);
  38. }
  39. onBindTransform() {
  40. if (this.current && !this.renderState.dimensions) {
  41. frame.postRender(this.updateDimensions);
  42. }
  43. }
  44. build(renderState, latestValues, props) {
  45. buildSVGAttrs(renderState, latestValues, this.isSVGTag, props.transformTemplate);
  46. }
  47. renderInstance(instance, renderState, styleProp, projection) {
  48. renderSVG(instance, renderState, styleProp, projection);
  49. }
  50. mount(instance) {
  51. this.isSVGTag = isSVGTag(instance.tagName);
  52. super.mount(instance);
  53. }
  54. }
  55. export { SVGVisualElement };