handleTransforms method

void handleTransforms(
  1. Element element,
  2. VMobject mobject
)

Implementation

void handleTransforms(Element element, VMobject mobject) {
  if (element.attributes['x'] != null && element.attributes['y'] != null) {
    var x = attributeToDouble(element.attributes['x']);
    var y = attributeToDouble(element.attributes['y']);

    mobject.shift(RIGHT * x + DOWN * y);
  }

  var transform = element.attributes['transform'] ?? '';
  var transformNames = [
    'matrix',
    'translate',
    'scale',
    'rotate',
    'skewX',
    'skewY'
  ];

  var transformRegex =
      RegExp([for (var x in transformNames) x + r'[^)]*\)'].join('|'));
  var transforms = transformRegex.allMatches(transform);

  var numberRegex = RegExp(r'[-+]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][-+]?\d+)?');

  for (var t in transforms) {
    var opData = t.group(0)!.split('(');
    var opName = opData[0].trim();
    var opArgs = [
      for (var x in numberRegex.allMatches(opData[1]))
        double.parse(x.group(0)!)
    ];

    switch (opName) {
      case 'matrix':
        var transformArgs = Array(values: [opArgs]).reshape(3, 2);
        var x = transformArgs[Tuple2(2, 0)];
        var y = transformArgs[Tuple2(2, 1)];

        var matrix = Array.identity(3);
        matrix[Tuple2(0, 0)] = transformArgs[Tuple2(0, 0)];
        matrix[Tuple2(0, 1)] = transformArgs[Tuple2(0, 1)];
        matrix[Tuple2(1, 0)] = transformArgs[Tuple2(1, 0)];
        matrix[Tuple2(1, 1)] = transformArgs[Tuple2(1, 1)];

        matrix[Tuple2(1, 0)] *= -1;
        matrix[Tuple2(1, 1)] *= -1;
        matrix[Tuple2(1, 2)] *= -1;

        matrix[Tuple2(0, 1)] *= -1;
        matrix[Tuple2(1, 1)] *= -1;
        matrix[Tuple2(2, 1)] *= -1;

        mobject.applyMatrix(matrix);
        mobject.shift(RIGHT * x + DOWN * y);

        break;

      case 'scale':
        var scaleValues = opArgs;

        if (scaleValues.length == 1) {
          mobject.scale(Vector3(scaleValues[0], scaleValues[0], 1),
              aboutPoint: ORIGIN);
        } else if (scaleValues.length == 2) {
          mobject.scale(Vector3(scaleValues[0], scaleValues[1], 1),
              aboutPoint: ORIGIN);
        }

        break;

      case 'translate':
        var x = opArgs[0];
        var y = opArgs.length == 2 ? opArgs[1] : 0;

        mobject.shift(RIGHT * x + DOWN * y);
        break;
    }
  }
}