@antv/g-lite 1.0.6 → 1.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.esm.js CHANGED
@@ -3,7 +3,7 @@ import { Syringe, GlobalContainer, singleton, inject, injectable, contrib, Modul
3
3
  export { GlobalContainer, Module, Syringe, inject, injectable, singleton } from 'mana-syringe';
4
4
  import RBush from 'rbush';
5
5
  export { default as RBush } from 'rbush';
6
- import { memoize, isString, isNumber, getTotalLength, clamp, isNil, isObject, normalizePath, path2Curve, getPathBBox, equalizeSegments, getDrawDirection, reverseCurve, clonePath, getRotatedCurve, isBoolean, isUndefined, getPointAtLength } from '@antv/util';
6
+ import { memoize, isString, isNumber, distanceSquareRoot, getTotalLength, clamp, isNil, isObject, normalizePath, path2Curve, getPathBBox, equalizeSegments, getDrawDirection, reverseCurve, clonePath, getRotatedCurve, isBoolean, isUndefined, getPointAtLength } from '@antv/util';
7
7
  import { color } from 'd3-color';
8
8
  import { vec3, mat4, vec4, mat3, quat as quat$1, vec2 } from 'gl-matrix';
9
9
  import { Polyline as Polyline$1, Line as Line$1 } from '@antv/g-math';
@@ -3250,717 +3250,747 @@ var TOUCH_TO_POINTER = {
3250
3250
  touchcancel: 'pointercancel'
3251
3251
  };
3252
3252
 
3253
- /**
3254
- * borrow from gradient-parser, but we delete some browser compatible prefix such as `-webkit-`
3255
- * @see https://github.com/rafaelcaricio/gradient-parser
3256
- */
3257
- function colorStopToString(colorStop) {
3258
- var type = colorStop.type,
3259
- value = colorStop.value;
3260
-
3261
- if (type === 'hex') {
3262
- return "#" + value;
3263
- } else if (type === 'literal') {
3264
- return value;
3265
- } else if (type === 'rgb') {
3266
- return "rgb(" + value.join(',') + ")";
3267
- } else {
3268
- return "rgba(" + value.join(',') + ")";
3253
+ function getAngle(angle) {
3254
+ if (angle === undefined) {
3255
+ return 0;
3256
+ } else if (angle > 360 || angle < -360) {
3257
+ return angle % 360;
3269
3258
  }
3270
- }
3271
- var parseGradient = function () {
3272
- var tokens = {
3273
- linearGradient: /^(linear\-gradient)/i,
3274
- repeatingLinearGradient: /^(repeating\-linear\-gradient)/i,
3275
- radialGradient: /^(radial\-gradient)/i,
3276
- repeatingRadialGradient: /^(repeating\-radial\-gradient)/i,
3277
-
3278
- /**
3279
- * @see https://projects.verou.me/conic-gradient/
3280
- */
3281
- conicGradient: /^(conic\-gradient)/i,
3282
- sideOrCorner: /^to (left (top|bottom)|right (top|bottom)|top (left|right)|bottom (left|right)|left|right|top|bottom)/i,
3283
- extentKeywords: /^(closest\-side|closest\-corner|farthest\-side|farthest\-corner|contain|cover)/,
3284
- positionKeywords: /^(left|center|right|top|bottom)/i,
3285
- pixelValue: /^(-?(([0-9]*\.[0-9]+)|([0-9]+\.?)))px/,
3286
- percentageValue: /^(-?(([0-9]*\.[0-9]+)|([0-9]+\.?)))\%/,
3287
- emValue: /^(-?(([0-9]*\.[0-9]+)|([0-9]+\.?)))em/,
3288
- angleValue: /^(-?(([0-9]*\.[0-9]+)|([0-9]+\.?)))deg/,
3289
- startCall: /^\(/,
3290
- endCall: /^\)/,
3291
- comma: /^,/,
3292
- hexColor: /^\#([0-9a-fA-F]+)/,
3293
- literalColor: /^([a-zA-Z]+)/,
3294
- rgbColor: /^rgb/i,
3295
- rgbaColor: /^rgba/i,
3296
- number: /^(([0-9]*\.[0-9]+)|([0-9]+\.?))/
3297
- };
3298
- var input = '';
3299
3259
 
3300
- function error(msg) {
3301
- throw new Error(input + ': ' + msg);
3260
+ return angle;
3261
+ }
3262
+ function createVec3(x, y, z) {
3263
+ if (y === void 0) {
3264
+ y = 0;
3302
3265
  }
3303
3266
 
3304
- function getAST() {
3305
- var ast = matchListDefinitions();
3306
-
3307
- if (input.length > 0) {
3308
- error('Invalid input not EOF');
3309
- }
3267
+ if (z === void 0) {
3268
+ z = 0;
3269
+ }
3310
3270
 
3311
- return ast;
3271
+ if (isNumber(x)) {
3272
+ return vec3.fromValues(x, y, z);
3312
3273
  }
3313
3274
 
3314
- function matchListDefinitions() {
3315
- return matchListing(matchDefinition);
3275
+ if (Array.isArray(x) && x.length === 3) {
3276
+ return vec3.clone(x);
3316
3277
  }
3317
3278
 
3318
- function matchDefinition() {
3319
- return matchGradient('linear-gradient', tokens.linearGradient, matchLinearOrientation) || matchGradient('repeating-linear-gradient', tokens.repeatingLinearGradient, matchLinearOrientation) || matchGradient('radial-gradient', tokens.radialGradient, matchListRadialOrientations) || matchGradient('repeating-radial-gradient', tokens.repeatingRadialGradient, matchListRadialOrientations) || matchGradient('conic-gradient', tokens.conicGradient, matchListRadialOrientations);
3279
+ return vec3.fromValues(x[0], x[1] || y, x[2] || z);
3280
+ }
3281
+ function deg2rad(deg) {
3282
+ return deg * (Math.PI / 180);
3283
+ }
3284
+ function rad2deg(rad) {
3285
+ return rad * (180 / Math.PI);
3286
+ }
3287
+ function grad2deg(grads) {
3288
+ grads = grads % 400;
3289
+
3290
+ if (grads < 0) {
3291
+ grads += 400;
3320
3292
  }
3321
3293
 
3322
- function matchGradient(gradientType, pattern, orientationMatcher) {
3323
- return matchCall(pattern, function (captures) {
3324
- var orientation = orientationMatcher();
3294
+ return grads / 400 * 360;
3295
+ }
3296
+ function deg2turn(deg) {
3297
+ return deg / 360;
3298
+ }
3299
+ function turn2deg(turn) {
3300
+ return 360 * turn;
3301
+ }
3325
3302
 
3326
- if (orientation) {
3327
- if (!scan(tokens.comma)) {
3328
- error('Missing comma before color stops');
3329
- }
3330
- }
3303
+ function getEulerFromQuat(out, quat) {
3304
+ var x = quat[0];
3305
+ var y = quat[1];
3306
+ var z = quat[2];
3307
+ var w = quat[3];
3308
+ var x2 = x * x;
3309
+ var y2 = y * y;
3310
+ var z2 = z * z;
3311
+ var w2 = w * w;
3312
+ var unit = x2 + y2 + z2 + w2;
3313
+ var test = x * w - y * z;
3331
3314
 
3332
- return {
3333
- type: gradientType,
3334
- orientation: orientation,
3335
- colorStops: matchListing(matchColorStop)
3336
- };
3337
- });
3338
- }
3315
+ if (test > 0.499995 * unit) {
3316
+ // TODO: Use glmatrix.EPSILON
3317
+ // singularity at the north pole
3318
+ out[0] = Math.PI / 2;
3319
+ out[1] = 2 * Math.atan2(y, x);
3320
+ out[2] = 0;
3321
+ } else if (test < -0.499995 * unit) {
3322
+ //TODO: Use glmatrix.EPSILON
3323
+ // singularity at the south pole
3324
+ out[0] = -Math.PI / 2;
3325
+ out[1] = 2 * Math.atan2(y, x);
3326
+ out[2] = 0;
3327
+ } else {
3328
+ out[0] = Math.asin(2 * (x * z - w * y));
3329
+ out[1] = Math.atan2(2 * (x * w + y * z), 1 - 2 * (z2 + w2));
3330
+ out[2] = Math.atan2(2 * (x * y + z * w), 1 - 2 * (y2 + z2));
3331
+ } // TODO: Return them as degrees and not as radians
3339
3332
 
3340
- function matchCall(pattern, callback) {
3341
- var captures = scan(pattern);
3342
3333
 
3343
- if (captures) {
3344
- if (!scan(tokens.startCall)) {
3345
- error('Missing (');
3346
- }
3334
+ return out;
3335
+ }
3347
3336
 
3348
- var result = callback(captures);
3337
+ function getEulerFromMat4(out, m) {
3338
+ var x;
3339
+ var z;
3340
+ var halfPi = Math.PI * 0.5;
3349
3341
 
3350
- if (!scan(tokens.endCall)) {
3351
- error('Missing )');
3352
- }
3342
+ var _mat4$getScaling = mat4.getScaling(vec3.create(), m),
3343
+ sx = _mat4$getScaling[0],
3344
+ sy = _mat4$getScaling[1],
3345
+ sz = _mat4$getScaling[2];
3353
3346
 
3354
- return result;
3347
+ var y = Math.asin(-m[2] / sx);
3348
+
3349
+ if (y < halfPi) {
3350
+ if (y > -halfPi) {
3351
+ x = Math.atan2(m[6] / sy, m[10] / sz);
3352
+ z = Math.atan2(m[1] / sx, m[0] / sx);
3353
+ } else {
3354
+ // Not a unique solution
3355
+ z = 0;
3356
+ x = -Math.atan2(m[4] / sy, m[5] / sy);
3355
3357
  }
3358
+ } else {
3359
+ // Not a unique solution
3360
+ z = 0;
3361
+ x = Math.atan2(m[4] / sy, m[5] / sy);
3356
3362
  }
3357
3363
 
3358
- function matchLinearOrientation() {
3359
- return matchSideOrCorner() || matchAngle();
3360
- }
3364
+ out[0] = x;
3365
+ out[1] = y;
3366
+ out[2] = z;
3367
+ return out;
3368
+ }
3369
+ /**
3370
+ * @see https://github.com/toji/gl-matrix/issues/329
3371
+ * @see https://doc.babylonjs.com/divingDeeper/mesh/transforms/center_origin/rotation_conventions
3372
+ */
3361
3373
 
3362
- function matchSideOrCorner() {
3363
- return match('directional', tokens.sideOrCorner, 1);
3364
- }
3365
3374
 
3366
- function matchAngle() {
3367
- return match('angular', tokens.angleValue, 1);
3375
+ function getEuler(out, quat) {
3376
+ if (quat.length === 16) {
3377
+ return getEulerFromMat4(out, quat);
3378
+ } else {
3379
+ return getEulerFromQuat(out, quat);
3368
3380
  }
3381
+ }
3382
+ function fromRotationTranslationScale(rotation, x, y, scaleX, scaleY) {
3383
+ var cos = Math.cos(rotation);
3384
+ var sin = Math.sin(rotation);
3385
+ return mat3.fromValues(scaleX * cos, scaleY * sin, 0, -scaleX * sin, scaleY * cos, 0, x, y, 1);
3386
+ }
3387
+ function makePerspective(out, left, right, top, bottom, near, far) {
3388
+ var x = 2 * near / (right - left);
3389
+ var y = 2 * near / (top - bottom);
3390
+ var a = (right + left) / (right - left);
3391
+ var b = (top + bottom) / (top - bottom);
3392
+ var c = -(far + near) / (far - near);
3393
+ var d = -2 * far * near / (far - near);
3394
+ out[0] = x;
3395
+ out[1] = 0;
3396
+ out[2] = 0;
3397
+ out[3] = 0;
3398
+ out[4] = 0;
3399
+ out[5] = y;
3400
+ out[6] = 0;
3401
+ out[7] = 0;
3402
+ out[8] = a;
3403
+ out[9] = b;
3404
+ out[10] = c;
3405
+ out[11] = -1;
3406
+ out[12] = 0;
3407
+ out[13] = 0;
3408
+ out[14] = d;
3409
+ out[15] = 0;
3410
+ return out;
3411
+ }
3412
+ function decompose(mat) {
3413
+ var row0x = mat[0];
3414
+ var row0y = mat[1];
3415
+ var row1x = mat[3];
3416
+ var row1y = mat[4]; // decompose 3x3 matrix
3417
+ // @see https://www.w3.org/TR/css-transforms-1/#decomposing-a-2d-matrix
3369
3418
 
3370
- function matchListRadialOrientations() {
3371
- var radialOrientations,
3372
- radialOrientation = matchRadialOrientation(),
3373
- lookaheadCache;
3374
-
3375
- if (radialOrientation) {
3376
- radialOrientations = [];
3377
- radialOrientations.push(radialOrientation);
3378
- lookaheadCache = input;
3419
+ var scalingX = Math.sqrt(row0x * row0x + row0y * row0y);
3420
+ var scalingY = Math.sqrt(row1x * row1x + row1y * row1y); // If determinant is negative, one axis was flipped.
3379
3421
 
3380
- if (scan(tokens.comma)) {
3381
- radialOrientation = matchRadialOrientation();
3422
+ var determinant = row0x * row1y - row0y * row1x;
3382
3423
 
3383
- if (radialOrientation) {
3384
- radialOrientations.push(radialOrientation);
3385
- } else {
3386
- input = lookaheadCache;
3387
- }
3388
- }
3424
+ if (determinant < 0) {
3425
+ // Flip axis with minimum unit vector dot product.
3426
+ if (row0x < row1y) {
3427
+ scalingX = -scalingX;
3428
+ } else {
3429
+ scalingY = -scalingY;
3389
3430
  }
3431
+ } // Renormalize matrix to remove scale.
3390
3432
 
3391
- return radialOrientations;
3433
+
3434
+ if (scalingX) {
3435
+ row0x *= 1 / scalingX;
3436
+ row0y *= 1 / scalingX;
3392
3437
  }
3393
3438
 
3394
- function matchRadialOrientation() {
3395
- var radialType = matchCircle() || matchEllipse();
3439
+ if (scalingY) {
3440
+ row1x *= 1 / scalingY;
3441
+ row1y *= 1 / scalingY;
3442
+ } // Compute rotation and renormalize matrix.
3396
3443
 
3397
- if (radialType) {
3398
- // @ts-ignore
3399
- radialType.at = matchAtPosition();
3400
- } else {
3401
- var extent = matchExtentKeyword();
3402
3444
 
3403
- if (extent) {
3404
- radialType = extent;
3405
- var positionAt = matchAtPosition();
3445
+ var rotation = Math.atan2(row0y, row0x);
3446
+ var angle = rad2deg(rotation);
3447
+ return [mat[6], mat[7], scalingX, scalingY, angle];
3448
+ }
3449
+ var tmp = mat4.create();
3450
+ var perspectiveMatrix = mat4.create();
3451
+ var tmpVec4 = vec4.create();
3452
+ var row = [vec3.create(), vec3.create(), vec3.create()];
3453
+ var pdum3 = vec3.create();
3454
+ /*
3455
+ Input: matrix ; a 4x4 matrix
3456
+ Output: translation ; a 3 component vector
3457
+ scale ; a 3 component vector
3458
+ skew ; skew factors XY,XZ,YZ represented as a 3 component vector
3459
+ perspective ; a 4 component vector
3460
+ quaternion ; a 4 component vector
3461
+ Returns false if the matrix cannot be decomposed, true if it can
3406
3462
 
3407
- if (positionAt) {
3408
- // @ts-ignore
3409
- radialType.at = positionAt;
3410
- }
3411
- } else {
3412
- var defaultPosition = matchPositioning();
3413
3463
 
3414
- if (defaultPosition) {
3415
- radialType = {
3416
- type: 'default-radial',
3417
- // @ts-ignore
3418
- at: defaultPosition
3419
- };
3420
- }
3421
- }
3422
- }
3464
+ References:
3465
+ https://github.com/kamicane/matrix3d/blob/master/lib/Matrix3d.js
3466
+ https://github.com/ChromiumWebApps/chromium/blob/master/ui/gfx/transform_util.cc
3467
+ http://www.w3.org/TR/css3-transforms/#decomposing-a-3d-matrix
3468
+ */
3423
3469
 
3424
- return radialType;
3425
- }
3470
+ function decomposeMat4(matrix, translation, scale, skew, perspective, quaternion) {
3471
+ //normalize, if not possible then bail out early
3472
+ if (!normalize(tmp, matrix)) return false; // perspectiveMatrix is used to solve for perspective, but it also provides
3473
+ // an easy way to test for singularity of the upper 3x3 component.
3426
3474
 
3427
- function matchCircle() {
3428
- var circle = match('shape', /^(circle)/i, 0);
3475
+ mat4.copy(perspectiveMatrix, tmp);
3476
+ perspectiveMatrix[3] = 0;
3477
+ perspectiveMatrix[7] = 0;
3478
+ perspectiveMatrix[11] = 0;
3479
+ perspectiveMatrix[15] = 1; // If the perspectiveMatrix is not invertible, we are also unable to
3480
+ // decompose, so we'll bail early. Constant taken from SkMatrix44::invert.
3429
3481
 
3430
- if (circle) {
3431
- // @ts-ignore
3432
- circle.style = matchLength() || matchExtentKeyword();
3433
- }
3482
+ if (Math.abs(mat4.determinant(perspectiveMatrix)) < 1e-8) return false;
3483
+ var a03 = tmp[3],
3484
+ a13 = tmp[7],
3485
+ a23 = tmp[11],
3486
+ a30 = tmp[12],
3487
+ a31 = tmp[13],
3488
+ a32 = tmp[14],
3489
+ a33 = tmp[15]; // First, isolate perspective.
3434
3490
 
3435
- return circle;
3436
- }
3491
+ if (a03 !== 0 || a13 !== 0 || a23 !== 0) {
3492
+ tmpVec4[0] = a03;
3493
+ tmpVec4[1] = a13;
3494
+ tmpVec4[2] = a23;
3495
+ tmpVec4[3] = a33; // Solve the equation by inverting perspectiveMatrix and multiplying
3496
+ // rightHandSide by the inverse.
3497
+ // resuing the perspectiveMatrix here since it's no longer needed
3498
+
3499
+ var ret = mat4.invert(perspectiveMatrix, perspectiveMatrix);
3500
+ if (!ret) return false;
3501
+ mat4.transpose(perspectiveMatrix, perspectiveMatrix); //multiply by transposed inverse perspective matrix, into perspective vec4
3502
+
3503
+ vec4.transformMat4(perspective, tmpVec4, perspectiveMatrix);
3504
+ } else {
3505
+ //no perspective
3506
+ perspective[0] = perspective[1] = perspective[2] = 0;
3507
+ perspective[3] = 1;
3508
+ } // Next take care of translation
3509
+
3510
+
3511
+ translation[0] = a30;
3512
+ translation[1] = a31;
3513
+ translation[2] = a32; // Now get scale and shear. 'row' is a 3 element array of 3 component vectors
3514
+
3515
+ mat3from4(row, tmp); // Compute X scale factor and normalize first row.
3516
+
3517
+ scale[0] = vec3.length(row[0]);
3518
+ vec3.normalize(row[0], row[0]); // Compute XY shear factor and make 2nd row orthogonal to 1st.
3519
+
3520
+ skew[0] = vec3.dot(row[0], row[1]);
3521
+ combine(row[1], row[1], row[0], 1.0, -skew[0]); // Now, compute Y scale and normalize 2nd row.
3522
+
3523
+ scale[1] = vec3.length(row[1]);
3524
+ vec3.normalize(row[1], row[1]);
3525
+ skew[0] /= scale[1]; // Compute XZ and YZ shears, orthogonalize 3rd row
3526
+
3527
+ skew[1] = vec3.dot(row[0], row[2]);
3528
+ combine(row[2], row[2], row[0], 1.0, -skew[1]);
3529
+ skew[2] = vec3.dot(row[1], row[2]);
3530
+ combine(row[2], row[2], row[1], 1.0, -skew[2]); // Next, get Z scale and normalize 3rd row.
3531
+
3532
+ scale[2] = vec3.length(row[2]);
3533
+ vec3.normalize(row[2], row[2]);
3534
+ skew[1] /= scale[2];
3535
+ skew[2] /= scale[2]; // At this point, the matrix (in rows) is orthonormal.
3536
+ // Check for a coordinate system flip. If the determinant
3537
+ // is -1, then negate the matrix and the scaling factors.
3437
3538
 
3438
- function matchEllipse() {
3439
- var ellipse = match('shape', /^(ellipse)/i, 0);
3539
+ vec3.cross(pdum3, row[1], row[2]);
3440
3540
 
3441
- if (ellipse) {
3442
- // @ts-ignore
3443
- ellipse.style = matchDistance() || matchExtentKeyword();
3541
+ if (vec3.dot(row[0], pdum3) < 0) {
3542
+ for (var i = 0; i < 3; i++) {
3543
+ scale[i] *= -1;
3544
+ row[i][0] *= -1;
3545
+ row[i][1] *= -1;
3546
+ row[i][2] *= -1;
3444
3547
  }
3548
+ } // Now, get the rotations out
3445
3549
 
3446
- return ellipse;
3447
- }
3448
3550
 
3449
- function matchExtentKeyword() {
3450
- return match('extent-keyword', tokens.extentKeywords, 1);
3451
- }
3551
+ quaternion[0] = 0.5 * Math.sqrt(Math.max(1 + row[0][0] - row[1][1] - row[2][2], 0));
3552
+ quaternion[1] = 0.5 * Math.sqrt(Math.max(1 - row[0][0] + row[1][1] - row[2][2], 0));
3553
+ quaternion[2] = 0.5 * Math.sqrt(Math.max(1 - row[0][0] - row[1][1] + row[2][2], 0));
3554
+ quaternion[3] = 0.5 * Math.sqrt(Math.max(1 + row[0][0] + row[1][1] + row[2][2], 0));
3555
+ if (row[2][1] > row[1][2]) quaternion[0] = -quaternion[0];
3556
+ if (row[0][2] > row[2][0]) quaternion[1] = -quaternion[1];
3557
+ if (row[1][0] > row[0][1]) quaternion[2] = -quaternion[2];
3558
+ return true;
3559
+ }
3452
3560
 
3453
- function matchAtPosition() {
3454
- if (match('position', /^at/, 0)) {
3455
- var positioning = matchPositioning();
3561
+ function normalize(out, mat) {
3562
+ var m44 = mat[15]; // Cannot normalize.
3456
3563
 
3457
- if (!positioning) {
3458
- error('Missing positioning value');
3459
- }
3564
+ if (m44 === 0) return false;
3565
+ var scale = 1 / m44;
3460
3566
 
3461
- return positioning;
3462
- }
3567
+ for (var i = 0; i < 16; i++) {
3568
+ out[i] = mat[i] * scale;
3463
3569
  }
3464
3570
 
3465
- function matchPositioning() {
3466
- var location = matchCoordinates();
3571
+ return true;
3572
+ } //gets upper-left of a 4x4 matrix into a 3x3 of vectors
3467
3573
 
3468
- if (location.x || location.y) {
3469
- return {
3470
- type: 'position',
3471
- value: location
3472
- };
3473
- }
3474
- }
3475
3574
 
3476
- function matchCoordinates() {
3477
- return {
3478
- x: matchDistance(),
3479
- y: matchDistance()
3480
- };
3481
- }
3575
+ function mat3from4(out, mat4x4) {
3576
+ out[0][0] = mat4x4[0];
3577
+ out[0][1] = mat4x4[1];
3578
+ out[0][2] = mat4x4[2];
3579
+ out[1][0] = mat4x4[4];
3580
+ out[1][1] = mat4x4[5];
3581
+ out[1][2] = mat4x4[6];
3582
+ out[2][0] = mat4x4[8];
3583
+ out[2][1] = mat4x4[9];
3584
+ out[2][2] = mat4x4[10];
3585
+ }
3482
3586
 
3483
- function matchListing(matcher) {
3484
- var captures = matcher();
3485
- var result = [];
3587
+ function combine(out, a, b, scale1, scale2) {
3588
+ out[0] = a[0] * scale1 + b[0] * scale2;
3589
+ out[1] = a[1] * scale1 + b[1] * scale2;
3590
+ out[2] = a[2] * scale1 + b[2] * scale2;
3591
+ }
3486
3592
 
3487
- if (captures) {
3488
- result.push(captures);
3593
+ /**
3594
+ * borrow from gradient-parser, but we delete some browser compatible prefix such as `-webkit-`
3595
+ * @see https://github.com/rafaelcaricio/gradient-parser
3596
+ */
3597
+ function colorStopToString(colorStop) {
3598
+ var type = colorStop.type,
3599
+ value = colorStop.value;
3489
3600
 
3490
- while (scan(tokens.comma)) {
3491
- captures = matcher();
3601
+ if (type === 'hex') {
3602
+ return "#" + value;
3603
+ } else if (type === 'literal') {
3604
+ return value;
3605
+ } else if (type === 'rgb') {
3606
+ return "rgb(" + value.join(',') + ")";
3607
+ } else {
3608
+ return "rgba(" + value.join(',') + ")";
3609
+ }
3610
+ }
3611
+ var parseGradient = function () {
3612
+ var tokens = {
3613
+ linearGradient: /^(linear\-gradient)/i,
3614
+ repeatingLinearGradient: /^(repeating\-linear\-gradient)/i,
3615
+ radialGradient: /^(radial\-gradient)/i,
3616
+ repeatingRadialGradient: /^(repeating\-radial\-gradient)/i,
3492
3617
 
3493
- if (captures) {
3494
- result.push(captures);
3495
- } else {
3496
- error('One extra comma');
3497
- }
3498
- }
3499
- }
3618
+ /**
3619
+ * @see https://projects.verou.me/conic-gradient/
3620
+ */
3621
+ conicGradient: /^(conic\-gradient)/i,
3622
+ sideOrCorner: /^to (left (top|bottom)|right (top|bottom)|top (left|right)|bottom (left|right)|left|right|top|bottom)/i,
3623
+ extentKeywords: /^(closest\-side|closest\-corner|farthest\-side|farthest\-corner|contain|cover)/,
3624
+ positionKeywords: /^(left|center|right|top|bottom)/i,
3625
+ pixelValue: /^(-?(([0-9]*\.[0-9]+)|([0-9]+\.?)))px/,
3626
+ percentageValue: /^(-?(([0-9]*\.[0-9]+)|([0-9]+\.?)))\%/,
3627
+ emValue: /^(-?(([0-9]*\.[0-9]+)|([0-9]+\.?)))em/,
3628
+ angleValue: /^(-?(([0-9]*\.[0-9]+)|([0-9]+\.?)))deg/,
3629
+ startCall: /^\(/,
3630
+ endCall: /^\)/,
3631
+ comma: /^,/,
3632
+ hexColor: /^\#([0-9a-fA-F]+)/,
3633
+ literalColor: /^([a-zA-Z]+)/,
3634
+ rgbColor: /^rgb/i,
3635
+ rgbaColor: /^rgba/i,
3636
+ number: /^(([0-9]*\.[0-9]+)|([0-9]+\.?))/
3637
+ };
3638
+ var input = '';
3500
3639
 
3501
- return result;
3640
+ function error(msg) {
3641
+ throw new Error(input + ': ' + msg);
3502
3642
  }
3503
3643
 
3504
- function matchColorStop() {
3505
- var color = matchColor();
3644
+ function getAST() {
3645
+ var ast = matchListDefinitions();
3506
3646
 
3507
- if (!color) {
3508
- error('Expected color definition');
3647
+ if (input.length > 0) {
3648
+ error('Invalid input not EOF');
3509
3649
  }
3510
3650
 
3511
- color.length = matchDistance();
3512
- return color;
3651
+ return ast;
3513
3652
  }
3514
3653
 
3515
- function matchColor() {
3516
- return matchHexColor() || matchRGBAColor() || matchRGBColor() || matchLiteralColor();
3654
+ function matchListDefinitions() {
3655
+ return matchListing(matchDefinition);
3517
3656
  }
3518
3657
 
3519
- function matchLiteralColor() {
3520
- return match('literal', tokens.literalColor, 0);
3658
+ function matchDefinition() {
3659
+ return matchGradient('linear-gradient', tokens.linearGradient, matchLinearOrientation) || matchGradient('repeating-linear-gradient', tokens.repeatingLinearGradient, matchLinearOrientation) || matchGradient('radial-gradient', tokens.radialGradient, matchListRadialOrientations) || matchGradient('repeating-radial-gradient', tokens.repeatingRadialGradient, matchListRadialOrientations) || matchGradient('conic-gradient', tokens.conicGradient, matchListRadialOrientations);
3521
3660
  }
3522
3661
 
3523
- function matchHexColor() {
3524
- return match('hex', tokens.hexColor, 1);
3525
- }
3662
+ function matchGradient(gradientType, pattern, orientationMatcher) {
3663
+ return matchCall(pattern, function (captures) {
3664
+ var orientation = orientationMatcher();
3526
3665
 
3527
- function matchRGBColor() {
3528
- return matchCall(tokens.rgbColor, function () {
3529
- return {
3530
- type: 'rgb',
3531
- value: matchListing(matchNumber)
3532
- };
3533
- });
3534
- }
3666
+ if (orientation) {
3667
+ if (!scan(tokens.comma)) {
3668
+ error('Missing comma before color stops');
3669
+ }
3670
+ }
3535
3671
 
3536
- function matchRGBAColor() {
3537
- return matchCall(tokens.rgbaColor, function () {
3538
3672
  return {
3539
- type: 'rgba',
3540
- value: matchListing(matchNumber)
3673
+ type: gradientType,
3674
+ orientation: orientation,
3675
+ colorStops: matchListing(matchColorStop)
3541
3676
  };
3542
3677
  });
3543
3678
  }
3544
3679
 
3545
- function matchNumber() {
3546
- return scan(tokens.number)[1];
3547
- }
3548
-
3549
- function matchDistance() {
3550
- return match('%', tokens.percentageValue, 1) || matchPositionKeyword() || matchLength();
3551
- }
3552
-
3553
- function matchPositionKeyword() {
3554
- return match('position-keyword', tokens.positionKeywords, 1);
3555
- }
3556
-
3557
- function matchLength() {
3558
- return match('px', tokens.pixelValue, 1) || match('em', tokens.emValue, 1);
3559
- }
3560
-
3561
- function match(type, pattern, captureIndex) {
3680
+ function matchCall(pattern, callback) {
3562
3681
  var captures = scan(pattern);
3563
3682
 
3564
3683
  if (captures) {
3565
- return {
3566
- type: type,
3567
- value: captures[captureIndex]
3568
- };
3569
- }
3570
- }
3571
-
3572
- function scan(regexp) {
3573
- var blankCaptures = /^[\n\r\t\s]+/.exec(input);
3574
-
3575
- if (blankCaptures) {
3576
- consume(blankCaptures[0].length);
3577
- }
3578
-
3579
- var captures = regexp.exec(input);
3580
-
3581
- if (captures) {
3582
- consume(captures[0].length);
3583
- }
3584
-
3585
- return captures;
3586
- }
3587
-
3588
- function consume(size) {
3589
- input = input.substring(size);
3590
- }
3591
-
3592
- return function (code) {
3593
- input = code;
3594
- return getAST();
3595
- };
3596
- }();
3597
- function computeLinearGradient(width, height, angle) {
3598
- var rad = angle * Math.PI / 180;
3599
- var rx = 0;
3600
- var ry = 0;
3601
- var rcx = rx + width / 2;
3602
- var rcy = ry + height / 2; // get the length of gradient line
3603
- // @see https://observablehq.com/@danburzo/css-gradient-line
3684
+ if (!scan(tokens.startCall)) {
3685
+ error('Missing (');
3686
+ }
3604
3687
 
3605
- var length = Math.abs(width * Math.cos(rad)) + Math.abs(height * Math.sin(rad));
3606
- var x1 = rcx - Math.cos(rad) * length / 2;
3607
- var y1 = rcy - Math.sin(rad) * length / 2;
3608
- var x2 = rcx + Math.cos(rad) * length / 2;
3609
- var y2 = rcy + Math.sin(rad) * length / 2;
3610
- return {
3611
- x1: x1,
3612
- y1: y1,
3613
- x2: x2,
3614
- y2: y2
3615
- };
3616
- }
3617
- function computeRadialGradient(width, height, cx, cy) {
3618
- var r = Math.sqrt(width * width + height * height) / 2;
3619
- return {
3620
- x: cx * width,
3621
- y: cy * height,
3622
- r: r
3623
- };
3624
- }
3688
+ var result = callback(captures);
3625
3689
 
3626
- function getAngle(angle) {
3627
- if (angle === undefined) {
3628
- return 0;
3629
- } else if (angle > 360 || angle < -360) {
3630
- return angle % 360;
3631
- }
3690
+ if (!scan(tokens.endCall)) {
3691
+ error('Missing )');
3692
+ }
3632
3693
 
3633
- return angle;
3634
- }
3635
- function createVec3(x, y, z) {
3636
- if (y === void 0) {
3637
- y = 0;
3694
+ return result;
3695
+ }
3638
3696
  }
3639
3697
 
3640
- if (z === void 0) {
3641
- z = 0;
3698
+ function matchLinearOrientation() {
3699
+ return matchSideOrCorner() || matchAngle();
3642
3700
  }
3643
3701
 
3644
- if (isNumber(x)) {
3645
- return vec3.fromValues(x, y, z);
3702
+ function matchSideOrCorner() {
3703
+ return match('directional', tokens.sideOrCorner, 1);
3646
3704
  }
3647
3705
 
3648
- if (Array.isArray(x) && x.length === 3) {
3649
- return vec3.clone(x);
3706
+ function matchAngle() {
3707
+ return match('angular', tokens.angleValue, 1);
3650
3708
  }
3651
3709
 
3652
- return vec3.fromValues(x[0], x[1] || y, x[2] || z);
3653
- }
3654
- function deg2rad(deg) {
3655
- return deg * (Math.PI / 180);
3656
- }
3657
- function rad2deg(rad) {
3658
- return rad * (180 / Math.PI);
3659
- }
3660
- function grad2deg(grads) {
3661
- grads = grads % 400;
3710
+ function matchListRadialOrientations() {
3711
+ var radialOrientations,
3712
+ radialOrientation = matchRadialOrientation(),
3713
+ lookaheadCache;
3662
3714
 
3663
- if (grads < 0) {
3664
- grads += 400;
3665
- }
3715
+ if (radialOrientation) {
3716
+ radialOrientations = [];
3717
+ radialOrientations.push(radialOrientation);
3718
+ lookaheadCache = input;
3666
3719
 
3667
- return grads / 400 * 360;
3668
- }
3669
- function deg2turn(deg) {
3670
- return deg / 360;
3671
- }
3672
- function turn2deg(turn) {
3673
- return 360 * turn;
3674
- }
3720
+ if (scan(tokens.comma)) {
3721
+ radialOrientation = matchRadialOrientation();
3675
3722
 
3676
- function getEulerFromQuat(out, quat) {
3677
- var x = quat[0];
3678
- var y = quat[1];
3679
- var z = quat[2];
3680
- var w = quat[3];
3681
- var x2 = x * x;
3682
- var y2 = y * y;
3683
- var z2 = z * z;
3684
- var w2 = w * w;
3685
- var unit = x2 + y2 + z2 + w2;
3686
- var test = x * w - y * z;
3723
+ if (radialOrientation) {
3724
+ radialOrientations.push(radialOrientation);
3725
+ } else {
3726
+ input = lookaheadCache;
3727
+ }
3728
+ }
3729
+ }
3687
3730
 
3688
- if (test > 0.499995 * unit) {
3689
- // TODO: Use glmatrix.EPSILON
3690
- // singularity at the north pole
3691
- out[0] = Math.PI / 2;
3692
- out[1] = 2 * Math.atan2(y, x);
3693
- out[2] = 0;
3694
- } else if (test < -0.499995 * unit) {
3695
- //TODO: Use glmatrix.EPSILON
3696
- // singularity at the south pole
3697
- out[0] = -Math.PI / 2;
3698
- out[1] = 2 * Math.atan2(y, x);
3699
- out[2] = 0;
3700
- } else {
3701
- out[0] = Math.asin(2 * (x * z - w * y));
3702
- out[1] = Math.atan2(2 * (x * w + y * z), 1 - 2 * (z2 + w2));
3703
- out[2] = Math.atan2(2 * (x * y + z * w), 1 - 2 * (y2 + z2));
3704
- } // TODO: Return them as degrees and not as radians
3731
+ return radialOrientations;
3732
+ }
3705
3733
 
3734
+ function matchRadialOrientation() {
3735
+ var radialType = matchCircle() || matchEllipse();
3706
3736
 
3707
- return out;
3708
- }
3737
+ if (radialType) {
3738
+ // @ts-ignore
3739
+ radialType.at = matchAtPosition();
3740
+ } else {
3741
+ var extent = matchExtentKeyword();
3709
3742
 
3710
- function getEulerFromMat4(out, m) {
3711
- var x;
3712
- var z;
3713
- var halfPi = Math.PI * 0.5;
3743
+ if (extent) {
3744
+ radialType = extent;
3745
+ var positionAt = matchAtPosition();
3714
3746
 
3715
- var _mat4$getScaling = mat4.getScaling(vec3.create(), m),
3716
- sx = _mat4$getScaling[0],
3717
- sy = _mat4$getScaling[1],
3718
- sz = _mat4$getScaling[2];
3747
+ if (positionAt) {
3748
+ // @ts-ignore
3749
+ radialType.at = positionAt;
3750
+ }
3751
+ } else {
3752
+ var defaultPosition = matchPositioning();
3719
3753
 
3720
- var y = Math.asin(-m[2] / sx);
3754
+ if (defaultPosition) {
3755
+ radialType = {
3756
+ type: 'default-radial',
3757
+ // @ts-ignore
3758
+ at: defaultPosition
3759
+ };
3760
+ }
3761
+ }
3762
+ }
3721
3763
 
3722
- if (y < halfPi) {
3723
- if (y > -halfPi) {
3724
- x = Math.atan2(m[6] / sy, m[10] / sz);
3725
- z = Math.atan2(m[1] / sx, m[0] / sx);
3726
- } else {
3727
- // Not a unique solution
3728
- z = 0;
3729
- x = -Math.atan2(m[4] / sy, m[5] / sy);
3764
+ return radialType;
3765
+ }
3766
+
3767
+ function matchCircle() {
3768
+ var circle = match('shape', /^(circle)/i, 0);
3769
+
3770
+ if (circle) {
3771
+ // @ts-ignore
3772
+ circle.style = matchLength() || matchExtentKeyword();
3730
3773
  }
3731
- } else {
3732
- // Not a unique solution
3733
- z = 0;
3734
- x = Math.atan2(m[4] / sy, m[5] / sy);
3774
+
3775
+ return circle;
3735
3776
  }
3736
3777
 
3737
- out[0] = x;
3738
- out[1] = y;
3739
- out[2] = z;
3740
- return out;
3741
- }
3742
- /**
3743
- * @see https://github.com/toji/gl-matrix/issues/329
3744
- * @see https://doc.babylonjs.com/divingDeeper/mesh/transforms/center_origin/rotation_conventions
3745
- */
3778
+ function matchEllipse() {
3779
+ var ellipse = match('shape', /^(ellipse)/i, 0);
3746
3780
 
3781
+ if (ellipse) {
3782
+ // @ts-ignore
3783
+ ellipse.style = matchDistance() || matchExtentKeyword();
3784
+ }
3747
3785
 
3748
- function getEuler(out, quat) {
3749
- if (quat.length === 16) {
3750
- return getEulerFromMat4(out, quat);
3751
- } else {
3752
- return getEulerFromQuat(out, quat);
3786
+ return ellipse;
3753
3787
  }
3754
- }
3755
- function fromRotationTranslationScale(rotation, x, y, scaleX, scaleY) {
3756
- var cos = Math.cos(rotation);
3757
- var sin = Math.sin(rotation);
3758
- return mat3.fromValues(scaleX * cos, scaleY * sin, 0, -scaleX * sin, scaleY * cos, 0, x, y, 1);
3759
- }
3760
- function makePerspective(out, left, right, top, bottom, near, far) {
3761
- var x = 2 * near / (right - left);
3762
- var y = 2 * near / (top - bottom);
3763
- var a = (right + left) / (right - left);
3764
- var b = (top + bottom) / (top - bottom);
3765
- var c = -(far + near) / (far - near);
3766
- var d = -2 * far * near / (far - near);
3767
- out[0] = x;
3768
- out[1] = 0;
3769
- out[2] = 0;
3770
- out[3] = 0;
3771
- out[4] = 0;
3772
- out[5] = y;
3773
- out[6] = 0;
3774
- out[7] = 0;
3775
- out[8] = a;
3776
- out[9] = b;
3777
- out[10] = c;
3778
- out[11] = -1;
3779
- out[12] = 0;
3780
- out[13] = 0;
3781
- out[14] = d;
3782
- out[15] = 0;
3783
- return out;
3784
- }
3785
- function decompose(mat) {
3786
- var row0x = mat[0];
3787
- var row0y = mat[1];
3788
- var row1x = mat[3];
3789
- var row1y = mat[4]; // decompose 3x3 matrix
3790
- // @see https://www.w3.org/TR/css-transforms-1/#decomposing-a-2d-matrix
3791
3788
 
3792
- var scalingX = Math.sqrt(row0x * row0x + row0y * row0y);
3793
- var scalingY = Math.sqrt(row1x * row1x + row1y * row1y); // If determinant is negative, one axis was flipped.
3789
+ function matchExtentKeyword() {
3790
+ return match('extent-keyword', tokens.extentKeywords, 1);
3791
+ }
3794
3792
 
3795
- var determinant = row0x * row1y - row0y * row1x;
3793
+ function matchAtPosition() {
3794
+ if (match('position', /^at/, 0)) {
3795
+ var positioning = matchPositioning();
3796
3796
 
3797
- if (determinant < 0) {
3798
- // Flip axis with minimum unit vector dot product.
3799
- if (row0x < row1y) {
3800
- scalingX = -scalingX;
3801
- } else {
3802
- scalingY = -scalingY;
3797
+ if (!positioning) {
3798
+ error('Missing positioning value');
3799
+ }
3800
+
3801
+ return positioning;
3803
3802
  }
3804
- } // Renormalize matrix to remove scale.
3803
+ }
3805
3804
 
3805
+ function matchPositioning() {
3806
+ var location = matchCoordinates();
3806
3807
 
3807
- if (scalingX) {
3808
- row0x *= 1 / scalingX;
3809
- row0y *= 1 / scalingX;
3808
+ if (location.x || location.y) {
3809
+ return {
3810
+ type: 'position',
3811
+ value: location
3812
+ };
3813
+ }
3810
3814
  }
3811
3815
 
3812
- if (scalingY) {
3813
- row1x *= 1 / scalingY;
3814
- row1y *= 1 / scalingY;
3815
- } // Compute rotation and renormalize matrix.
3816
-
3816
+ function matchCoordinates() {
3817
+ return {
3818
+ x: matchDistance(),
3819
+ y: matchDistance()
3820
+ };
3821
+ }
3817
3822
 
3818
- var rotation = Math.atan2(row0y, row0x);
3819
- var angle = rad2deg(rotation);
3820
- return [mat[6], mat[7], scalingX, scalingY, angle];
3821
- }
3822
- var tmp = mat4.create();
3823
- var perspectiveMatrix = mat4.create();
3824
- var tmpVec4 = vec4.create();
3825
- var row = [vec3.create(), vec3.create(), vec3.create()];
3826
- var pdum3 = vec3.create();
3827
- /*
3828
- Input: matrix ; a 4x4 matrix
3829
- Output: translation ; a 3 component vector
3830
- scale ; a 3 component vector
3831
- skew ; skew factors XY,XZ,YZ represented as a 3 component vector
3832
- perspective ; a 4 component vector
3833
- quaternion ; a 4 component vector
3834
- Returns false if the matrix cannot be decomposed, true if it can
3823
+ function matchListing(matcher) {
3824
+ var captures = matcher();
3825
+ var result = [];
3835
3826
 
3827
+ if (captures) {
3828
+ result.push(captures);
3836
3829
 
3837
- References:
3838
- https://github.com/kamicane/matrix3d/blob/master/lib/Matrix3d.js
3839
- https://github.com/ChromiumWebApps/chromium/blob/master/ui/gfx/transform_util.cc
3840
- http://www.w3.org/TR/css3-transforms/#decomposing-a-3d-matrix
3841
- */
3830
+ while (scan(tokens.comma)) {
3831
+ captures = matcher();
3842
3832
 
3843
- function decomposeMat4(matrix, translation, scale, skew, perspective, quaternion) {
3844
- //normalize, if not possible then bail out early
3845
- if (!normalize(tmp, matrix)) return false; // perspectiveMatrix is used to solve for perspective, but it also provides
3846
- // an easy way to test for singularity of the upper 3x3 component.
3833
+ if (captures) {
3834
+ result.push(captures);
3835
+ } else {
3836
+ error('One extra comma');
3837
+ }
3838
+ }
3839
+ }
3847
3840
 
3848
- mat4.copy(perspectiveMatrix, tmp);
3849
- perspectiveMatrix[3] = 0;
3850
- perspectiveMatrix[7] = 0;
3851
- perspectiveMatrix[11] = 0;
3852
- perspectiveMatrix[15] = 1; // If the perspectiveMatrix is not invertible, we are also unable to
3853
- // decompose, so we'll bail early. Constant taken from SkMatrix44::invert.
3841
+ return result;
3842
+ }
3854
3843
 
3855
- if (Math.abs(mat4.determinant(perspectiveMatrix)) < 1e-8) return false;
3856
- var a03 = tmp[3],
3857
- a13 = tmp[7],
3858
- a23 = tmp[11],
3859
- a30 = tmp[12],
3860
- a31 = tmp[13],
3861
- a32 = tmp[14],
3862
- a33 = tmp[15]; // First, isolate perspective.
3844
+ function matchColorStop() {
3845
+ var color = matchColor();
3863
3846
 
3864
- if (a03 !== 0 || a13 !== 0 || a23 !== 0) {
3865
- tmpVec4[0] = a03;
3866
- tmpVec4[1] = a13;
3867
- tmpVec4[2] = a23;
3868
- tmpVec4[3] = a33; // Solve the equation by inverting perspectiveMatrix and multiplying
3869
- // rightHandSide by the inverse.
3870
- // resuing the perspectiveMatrix here since it's no longer needed
3847
+ if (!color) {
3848
+ error('Expected color definition');
3849
+ }
3871
3850
 
3872
- var ret = mat4.invert(perspectiveMatrix, perspectiveMatrix);
3873
- if (!ret) return false;
3874
- mat4.transpose(perspectiveMatrix, perspectiveMatrix); //multiply by transposed inverse perspective matrix, into perspective vec4
3851
+ color.length = matchDistance();
3852
+ return color;
3853
+ }
3875
3854
 
3876
- vec4.transformMat4(perspective, tmpVec4, perspectiveMatrix);
3877
- } else {
3878
- //no perspective
3879
- perspective[0] = perspective[1] = perspective[2] = 0;
3880
- perspective[3] = 1;
3881
- } // Next take care of translation
3855
+ function matchColor() {
3856
+ return matchHexColor() || matchRGBAColor() || matchRGBColor() || matchLiteralColor();
3857
+ }
3882
3858
 
3859
+ function matchLiteralColor() {
3860
+ return match('literal', tokens.literalColor, 0);
3861
+ }
3883
3862
 
3884
- translation[0] = a30;
3885
- translation[1] = a31;
3886
- translation[2] = a32; // Now get scale and shear. 'row' is a 3 element array of 3 component vectors
3863
+ function matchHexColor() {
3864
+ return match('hex', tokens.hexColor, 1);
3865
+ }
3887
3866
 
3888
- mat3from4(row, tmp); // Compute X scale factor and normalize first row.
3867
+ function matchRGBColor() {
3868
+ return matchCall(tokens.rgbColor, function () {
3869
+ return {
3870
+ type: 'rgb',
3871
+ value: matchListing(matchNumber)
3872
+ };
3873
+ });
3874
+ }
3889
3875
 
3890
- scale[0] = vec3.length(row[0]);
3891
- vec3.normalize(row[0], row[0]); // Compute XY shear factor and make 2nd row orthogonal to 1st.
3876
+ function matchRGBAColor() {
3877
+ return matchCall(tokens.rgbaColor, function () {
3878
+ return {
3879
+ type: 'rgba',
3880
+ value: matchListing(matchNumber)
3881
+ };
3882
+ });
3883
+ }
3892
3884
 
3893
- skew[0] = vec3.dot(row[0], row[1]);
3894
- combine(row[1], row[1], row[0], 1.0, -skew[0]); // Now, compute Y scale and normalize 2nd row.
3885
+ function matchNumber() {
3886
+ return scan(tokens.number)[1];
3887
+ }
3895
3888
 
3896
- scale[1] = vec3.length(row[1]);
3897
- vec3.normalize(row[1], row[1]);
3898
- skew[0] /= scale[1]; // Compute XZ and YZ shears, orthogonalize 3rd row
3889
+ function matchDistance() {
3890
+ return match('%', tokens.percentageValue, 1) || matchPositionKeyword() || matchLength();
3891
+ }
3899
3892
 
3900
- skew[1] = vec3.dot(row[0], row[2]);
3901
- combine(row[2], row[2], row[0], 1.0, -skew[1]);
3902
- skew[2] = vec3.dot(row[1], row[2]);
3903
- combine(row[2], row[2], row[1], 1.0, -skew[2]); // Next, get Z scale and normalize 3rd row.
3893
+ function matchPositionKeyword() {
3894
+ return match('position-keyword', tokens.positionKeywords, 1);
3895
+ }
3904
3896
 
3905
- scale[2] = vec3.length(row[2]);
3906
- vec3.normalize(row[2], row[2]);
3907
- skew[1] /= scale[2];
3908
- skew[2] /= scale[2]; // At this point, the matrix (in rows) is orthonormal.
3909
- // Check for a coordinate system flip. If the determinant
3910
- // is -1, then negate the matrix and the scaling factors.
3897
+ function matchLength() {
3898
+ return match('px', tokens.pixelValue, 1) || match('em', tokens.emValue, 1);
3899
+ }
3911
3900
 
3912
- vec3.cross(pdum3, row[1], row[2]);
3901
+ function match(type, pattern, captureIndex) {
3902
+ var captures = scan(pattern);
3913
3903
 
3914
- if (vec3.dot(row[0], pdum3) < 0) {
3915
- for (var i = 0; i < 3; i++) {
3916
- scale[i] *= -1;
3917
- row[i][0] *= -1;
3918
- row[i][1] *= -1;
3919
- row[i][2] *= -1;
3904
+ if (captures) {
3905
+ return {
3906
+ type: type,
3907
+ value: captures[captureIndex]
3908
+ };
3920
3909
  }
3921
- } // Now, get the rotations out
3910
+ }
3922
3911
 
3912
+ function scan(regexp) {
3913
+ var blankCaptures = /^[\n\r\t\s]+/.exec(input);
3923
3914
 
3924
- quaternion[0] = 0.5 * Math.sqrt(Math.max(1 + row[0][0] - row[1][1] - row[2][2], 0));
3925
- quaternion[1] = 0.5 * Math.sqrt(Math.max(1 - row[0][0] + row[1][1] - row[2][2], 0));
3926
- quaternion[2] = 0.5 * Math.sqrt(Math.max(1 - row[0][0] - row[1][1] + row[2][2], 0));
3927
- quaternion[3] = 0.5 * Math.sqrt(Math.max(1 + row[0][0] + row[1][1] + row[2][2], 0));
3928
- if (row[2][1] > row[1][2]) quaternion[0] = -quaternion[0];
3929
- if (row[0][2] > row[2][0]) quaternion[1] = -quaternion[1];
3930
- if (row[1][0] > row[0][1]) quaternion[2] = -quaternion[2];
3931
- return true;
3932
- }
3915
+ if (blankCaptures) {
3916
+ consume(blankCaptures[0].length);
3917
+ }
3933
3918
 
3934
- function normalize(out, mat) {
3935
- var m44 = mat[15]; // Cannot normalize.
3919
+ var captures = regexp.exec(input);
3936
3920
 
3937
- if (m44 === 0) return false;
3938
- var scale = 1 / m44;
3921
+ if (captures) {
3922
+ consume(captures[0].length);
3923
+ }
3939
3924
 
3940
- for (var i = 0; i < 16; i++) {
3941
- out[i] = mat[i] * scale;
3925
+ return captures;
3942
3926
  }
3943
3927
 
3944
- return true;
3945
- } //gets upper-left of a 4x4 matrix into a 3x3 of vectors
3928
+ function consume(size) {
3929
+ input = input.substring(size);
3930
+ }
3946
3931
 
3932
+ return function (code) {
3933
+ input = code;
3934
+ return getAST();
3935
+ };
3936
+ }();
3937
+ function computeLinearGradient(width, height, angle) {
3938
+ var rad = deg2rad(angle.value);
3939
+ var rx = 0;
3940
+ var ry = 0;
3941
+ var rcx = rx + width / 2;
3942
+ var rcy = ry + height / 2; // get the length of gradient line
3943
+ // @see https://observablehq.com/@danburzo/css-gradient-line
3947
3944
 
3948
- function mat3from4(out, mat4x4) {
3949
- out[0][0] = mat4x4[0];
3950
- out[0][1] = mat4x4[1];
3951
- out[0][2] = mat4x4[2];
3952
- out[1][0] = mat4x4[4];
3953
- out[1][1] = mat4x4[5];
3954
- out[1][2] = mat4x4[6];
3955
- out[2][0] = mat4x4[8];
3956
- out[2][1] = mat4x4[9];
3957
- out[2][2] = mat4x4[10];
3945
+ var length = Math.abs(width * Math.cos(rad)) + Math.abs(height * Math.sin(rad));
3946
+ var x1 = rcx - Math.cos(rad) * length / 2;
3947
+ var y1 = rcy - Math.sin(rad) * length / 2;
3948
+ var x2 = rcx + Math.cos(rad) * length / 2;
3949
+ var y2 = rcy + Math.sin(rad) * length / 2;
3950
+ return {
3951
+ x1: x1,
3952
+ y1: y1,
3953
+ x2: x2,
3954
+ y2: y2
3955
+ };
3958
3956
  }
3957
+ function computeRadialGradient(width, height, cx, cy, size) {
3958
+ // 'px'
3959
+ var x = cx.value;
3960
+ var y = cy.value; // TODO: 'em'
3961
+ // '%'
3962
+
3963
+ if (cx.unit === UnitType.kPercentage) {
3964
+ x = cx.value / 100 * width;
3965
+ }
3966
+
3967
+ if (cy.unit === UnitType.kPercentage) {
3968
+ y = cy.value / 100 * height;
3969
+ } // default to farthest-side
3970
+
3971
+
3972
+ var r = Math.max(distanceSquareRoot([0, 0], [x, y]), distanceSquareRoot([0, height], [x, y]), distanceSquareRoot([width, height], [x, y]), distanceSquareRoot([width, 0], [x, y]));
3973
+
3974
+ if (size) {
3975
+ if (size instanceof CSSUnitValue) {
3976
+ r = size.value;
3977
+ } else if (size instanceof CSSKeywordValue) {
3978
+ // @see https://developer.mozilla.org/zh-CN/docs/Web/CSS/CSS_Images/Using_CSS_gradients#example_closest-side_for_circles
3979
+ if (size.value === 'closest-side') {
3980
+ r = Math.min(x, width - x, y, height - y);
3981
+ } else if (size.value === 'farthest-side') {
3982
+ r = Math.max(x, width - x, y, height - y);
3983
+ } else if (size.value === 'closest-corner') {
3984
+ r = Math.min(distanceSquareRoot([0, 0], [x, y]), distanceSquareRoot([0, height], [x, y]), distanceSquareRoot([width, height], [x, y]), distanceSquareRoot([width, 0], [x, y]));
3985
+ }
3986
+ }
3987
+ }
3959
3988
 
3960
- function combine(out, a, b, scale1, scale2) {
3961
- out[0] = a[0] * scale1 + b[0] * scale2;
3962
- out[1] = a[1] * scale1 + b[1] * scale2;
3963
- out[2] = a[2] * scale1 + b[2] * scale2;
3989
+ return {
3990
+ x: x,
3991
+ y: y,
3992
+ r: r
3993
+ };
3964
3994
  }
3965
3995
 
3966
3996
  var Shape;
@@ -5530,6 +5560,7 @@ function spaceColorStops(colorStops) {
5530
5560
  for (var i = 1; i < length; i++) {
5531
5561
  var _colorStops$i$length;
5532
5562
 
5563
+ // support '%' & 'px'
5533
5564
  var offset = (_colorStops$i$length = colorStops[i].length) === null || _colorStops$i$length === void 0 ? void 0 : _colorStops$i$length.value;
5534
5565
 
5535
5566
  if (!isNil(offset) && !isNil(previousOffset)) {
@@ -5571,11 +5602,13 @@ var angleToDeg = memoize(function (orientation) {
5571
5602
  angle = SideOrCornerToDegMap[orientation.value] || 0;
5572
5603
  }
5573
5604
 
5574
- return angle;
5605
+ return getOrCreateUnitValue(angle, 'deg');
5575
5606
  });
5576
- var positonToPercentage = memoize(function (position) {
5577
- var cx = 0.5;
5578
- var cy = 0.5;
5607
+ var positonToCSSUnitValue = memoize(function (position) {
5608
+ var cx = 50;
5609
+ var cy = 50;
5610
+ var unitX = '%';
5611
+ var unitY = '%';
5579
5612
 
5580
5613
  if ((position === null || position === void 0 ? void 0 : position.type) === 'position') {
5581
5614
  var _position$value = position.value,
@@ -5586,13 +5619,13 @@ var positonToPercentage = memoize(function (position) {
5586
5619
  if (x.value === 'left') {
5587
5620
  cx = 0;
5588
5621
  } else if (x.value === 'center') {
5589
- cx = 0.5;
5622
+ cx = 50;
5590
5623
  } else if (x.value === 'right') {
5591
- cx = 1;
5624
+ cx = 100;
5592
5625
  } else if (x.value === 'top') {
5593
5626
  cy = 0;
5594
5627
  } else if (x.value === 'bottom') {
5595
- cy = 1;
5628
+ cy = 100;
5596
5629
  }
5597
5630
  }
5598
5631
 
@@ -5600,28 +5633,30 @@ var positonToPercentage = memoize(function (position) {
5600
5633
  if (y.value === 'left') {
5601
5634
  cx = 0;
5602
5635
  } else if (y.value === 'center') {
5603
- cy = 0.5;
5636
+ cy = 50;
5604
5637
  } else if (y.value === 'right') {
5605
- cx = 1;
5638
+ cx = 100;
5606
5639
  } else if (y.value === 'top') {
5607
5640
  cy = 0;
5608
5641
  } else if (y.value === 'bottom') {
5609
- cy = 1;
5642
+ cy = 100;
5610
5643
  }
5611
5644
  }
5612
5645
 
5613
- if ((x === null || x === void 0 ? void 0 : x.type) === '%') {
5614
- cx = Number(x.value) / 100;
5646
+ if ((x === null || x === void 0 ? void 0 : x.type) === 'px' || (x === null || x === void 0 ? void 0 : x.type) === '%' || (x === null || x === void 0 ? void 0 : x.type) === 'em') {
5647
+ unitX = x === null || x === void 0 ? void 0 : x.type;
5648
+ cx = Number(x.value);
5615
5649
  }
5616
5650
 
5617
- if ((y === null || y === void 0 ? void 0 : y.type) === '%') {
5618
- cy = Number(x.value) / 100;
5651
+ if ((y === null || y === void 0 ? void 0 : y.type) === 'px' || (y === null || y === void 0 ? void 0 : y.type) === '%' || (y === null || y === void 0 ? void 0 : y.type) === 'em') {
5652
+ unitY = y === null || y === void 0 ? void 0 : y.type;
5653
+ cy = Number(y.value);
5619
5654
  }
5620
5655
  }
5621
5656
 
5622
5657
  return {
5623
- cx: cx,
5624
- cy: cy
5658
+ cx: getOrCreateUnitValue(cx, unitX),
5659
+ cy: getOrCreateUnitValue(cy, unitY)
5625
5660
  };
5626
5661
  });
5627
5662
  var parseGradient$1 = memoize(function (colorStr) {
@@ -5634,14 +5669,16 @@ var parseGradient$1 = memoize(function (colorStr) {
5634
5669
  spaceColorStops(colorStops);
5635
5670
  var steps = colorStops.map(function (colorStop) {
5636
5671
  // TODO: only support % for now, should calc percentage of axis length when using px/em
5637
- return [Number(colorStop.length.value) / 100, colorStopToString(colorStop)];
5672
+ return {
5673
+ offset: getOrCreateUnitValue(colorStop.length.value, '%'),
5674
+ color: colorStopToString(colorStop)
5675
+ };
5638
5676
  });
5639
5677
 
5640
5678
  if (type === 'linear-gradient') {
5641
5679
  return new CSSGradientValue(GradientType.LinearGradient, {
5642
- angle: orientation ? angleToDeg(orientation) : 0,
5643
- steps: steps,
5644
- hash: colorStr
5680
+ angle: orientation ? angleToDeg(orientation) : Odeg,
5681
+ steps: steps
5645
5682
  });
5646
5683
  } else if (type === 'radial-gradient') {
5647
5684
  if (!orientation) {
@@ -5652,17 +5689,33 @@ var parseGradient$1 = memoize(function (colorStr) {
5652
5689
  }
5653
5690
 
5654
5691
  if (orientation[0].type === 'shape' && orientation[0].value === 'circle') {
5655
- var _positonToPercentage = positonToPercentage(orientation[0].at),
5656
- cx = _positonToPercentage.cx,
5657
- cy = _positonToPercentage.cy;
5692
+ var _positonToCSSUnitValu = positonToCSSUnitValue(orientation[0].at),
5693
+ cx = _positonToCSSUnitValu.cx,
5694
+ cy = _positonToCSSUnitValu.cy;
5695
+
5696
+ var size;
5697
+
5698
+ if (orientation[0].style) {
5699
+ var _orientation$0$style = orientation[0].style,
5700
+ _type = _orientation$0$style.type,
5701
+ value = _orientation$0$style.value;
5702
+
5703
+ if (_type === 'extent-keyword') {
5704
+ size = getOrCreateKeyword(value);
5705
+ } else {
5706
+ size = getOrCreateUnitValue(value, _type);
5707
+ }
5708
+ }
5658
5709
 
5659
5710
  return new CSSGradientValue(GradientType.RadialGradient, {
5660
5711
  cx: cx,
5661
5712
  cy: cy,
5662
- steps: steps,
5663
- hash: colorStr
5713
+ size: size,
5714
+ steps: steps
5664
5715
  });
5665
- } // } else if (type === 'repeating-linear-gradient') {
5716
+ } // TODO: support ellipse shape
5717
+ // TODO: repeating-linear-gradient & repeating-radial-gradient
5718
+ // } else if (type === 'repeating-linear-gradient') {
5666
5719
  // } else if (type === 'repeating-radial-gradient') {
5667
5720
 
5668
5721
  }
@@ -5683,13 +5736,15 @@ var parseGradient$1 = memoize(function (colorStr) {
5683
5736
  return stop.split(':');
5684
5737
  })) || [];
5685
5738
  return [new CSSGradientValue(GradientType.LinearGradient, {
5686
- angle: parseFloat(arr[1]),
5739
+ angle: getOrCreateUnitValue(parseFloat(arr[1]), 'deg'),
5687
5740
  steps: steps.map(function (_ref2) {
5688
5741
  var offset = _ref2[0],
5689
5742
  color = _ref2[1];
5690
- return [Number(offset), color];
5691
- }),
5692
- hash: colorStr
5743
+ return {
5744
+ offset: getOrCreateUnitValue(Number(offset) * 100, '%'),
5745
+ color: color
5746
+ };
5747
+ })
5693
5748
  })];
5694
5749
  }
5695
5750
  } else if (type === 'r') {
@@ -5718,14 +5773,16 @@ function parseRadialGradient(gradientStr) {
5718
5773
  return stop.split(':');
5719
5774
  })) || [];
5720
5775
  return {
5721
- cx: 0.5,
5722
- cy: 0.5,
5776
+ cx: getOrCreateUnitValue(50, '%'),
5777
+ cy: getOrCreateUnitValue(50, '%'),
5723
5778
  steps: steps.map(function (_ref3) {
5724
5779
  var offset = _ref3[0],
5725
5780
  color = _ref3[1];
5726
- return [Number(offset), color];
5727
- }),
5728
- hash: gradientStr
5781
+ return {
5782
+ offset: getOrCreateUnitValue(Number(offset) * 100, '%'),
5783
+ color: color
5784
+ };
5785
+ })
5729
5786
  };
5730
5787
  }
5731
5788
 
@@ -9367,7 +9424,10 @@ MutationEvent.ADDITION = 2;
9367
9424
  MutationEvent.MODIFICATION = 1;
9368
9425
  MutationEvent.REMOVAL = 3;
9369
9426
 
9370
- var entityCounter = 0; // let this.sceneGraphService: SceneGraphService;
9427
+ var entityCounter = 0;
9428
+ function resetEntityCounter() {
9429
+ entityCounter = 0;
9430
+ } // let this.sceneGraphService: SceneGraphService;
9371
9431
 
9372
9432
  var childInsertedEvent = new CustomEvent(ElementEvent.CHILD_INSERTED, {
9373
9433
  child: null
@@ -17762,6 +17822,35 @@ var EventPlugin = EventPlugin_1 = (_class$2 = /*#__PURE__*/function () {
17762
17822
  renderingService.hooks.pointerOut.tap(EventPlugin_1.tag, this.onPointerMove);
17763
17823
  };
17764
17824
 
17825
+ _proto.getViewportXY = function getViewportXY(nativeEvent) {
17826
+ var x;
17827
+ var y;
17828
+ /**
17829
+ * Should account for CSS Transform applied on container.
17830
+ * @see https://github.com/antvis/G/issues/1161
17831
+ * @see https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent/offsetX
17832
+ */
17833
+
17834
+ var offsetX = nativeEvent.offsetX,
17835
+ offsetY = nativeEvent.offsetY,
17836
+ clientX = nativeEvent.clientX,
17837
+ clientY = nativeEvent.clientY;
17838
+
17839
+ if (this.canvasConfig.supportsCSSTransform && !isNil(offsetX) && !isNil(offsetY)) {
17840
+ x = offsetX;
17841
+ y = offsetY;
17842
+ } else {
17843
+ var point = this.eventService.client2Viewport(new Point(clientX, clientY));
17844
+ x = point.x;
17845
+ y = point.y;
17846
+ }
17847
+
17848
+ return {
17849
+ x: x,
17850
+ y: y
17851
+ };
17852
+ };
17853
+
17765
17854
  _proto.bootstrapEvent = function bootstrapEvent(event, nativeEvent, view) {
17766
17855
  event.view = view;
17767
17856
  event.originalEvent = null;
@@ -17778,9 +17867,9 @@ var EventPlugin = EventPlugin_1 = (_class$2 = /*#__PURE__*/function () {
17778
17867
  event.twist = nativeEvent.twist;
17779
17868
  this.transferMouseData(event, nativeEvent);
17780
17869
 
17781
- var _this$eventService$cl = this.eventService.client2Viewport(new Point(nativeEvent.clientX, nativeEvent.clientY)),
17782
- x = _this$eventService$cl.x,
17783
- y = _this$eventService$cl.y;
17870
+ var _this$getViewportXY = this.getViewportXY(nativeEvent),
17871
+ x = _this$getViewportXY.x,
17872
+ y = _this$getViewportXY.y;
17784
17873
 
17785
17874
  event.viewport.x = x;
17786
17875
  event.viewport.y = y;
@@ -17818,9 +17907,9 @@ var EventPlugin = EventPlugin_1 = (_class$2 = /*#__PURE__*/function () {
17818
17907
  event.deltaY = nativeEvent.deltaY;
17819
17908
  event.deltaZ = nativeEvent.deltaZ;
17820
17909
 
17821
- var _this$eventService$cl2 = this.eventService.client2Viewport(new Point(nativeEvent.clientX, nativeEvent.clientY)),
17822
- x = _this$eventService$cl2.x,
17823
- y = _this$eventService$cl2.y;
17910
+ var _this$getViewportXY2 = this.getViewportXY(nativeEvent),
17911
+ x = _this$getViewportXY2.x,
17912
+ y = _this$getViewportXY2.y;
17824
17913
 
17825
17914
  event.viewport.x = x;
17826
17915
  event.viewport.y = y;
@@ -18362,6 +18451,7 @@ var Canvas = /*#__PURE__*/function (_EventTarget) {
18362
18451
  createImage = config.createImage,
18363
18452
  supportsPointerEvents = config.supportsPointerEvents,
18364
18453
  supportsTouchEvents = config.supportsTouchEvents,
18454
+ supportsCSSTransform = config.supportsCSSTransform,
18365
18455
  isTouchEvent = config.isTouchEvent,
18366
18456
  isMouseEvent = config.isMouseEvent;
18367
18457
  cleanExistedCanvas(container, _assertThisInitialized(_this));
@@ -18410,7 +18500,8 @@ var Canvas = /*#__PURE__*/function (_EventTarget) {
18410
18500
  cursor: cursor || 'default',
18411
18501
  background: background || 'transparent',
18412
18502
  createImage: createImage,
18413
- document: document
18503
+ document: document,
18504
+ supportsCSSTransform: supportsCSSTransform
18414
18505
  });
18415
18506
 
18416
18507
  _this.initDefaultCamera(canvasWidth, canvasHeight);
@@ -18912,4 +19003,4 @@ GlobalContainer.load(containerModule); // export const sceneGraphService = Globa
18912
19003
 
18913
19004
  var styleValueRegistry = GlobalContainer.get(StyleValueRegistry); // export const displayObjectPool = GlobalContainer.get(DisplayObjectPool);
18914
19005
 
18915
- export { AABB, AbstractRenderer, AbstractRendererPlugin, AnimationTimelineToken, BUILT_IN_PROPERTIES, CSS, CSSGradientValue, CSSKeywordValue, CSSRGB, CSSStyleValue, CSSUnitValue, Camera, CameraContribution, CameraEvent, CameraProjectionMode, CameraTrackingMode, CameraType, Canvas, CanvasConfig, CanvasEvent, Circle, CircleUpdater, ContextService, CustomElement, CustomElementRegistry, CustomEvent, DefaultCamera, DefaultSceneGraphSelector, DefaultSceneGraphService, DisplayObject, DisplayObjectPool, Document, ERROR_MSG_METHOD_NOT_IMPLEMENTED, Element, ElementEvent, Ellipse, EllipseUpdater, EventService, EventTarget, FederatedEvent, FederatedMouseEvent, FederatedPointerEvent, FederatedWheelEvent, Frustum, GeometryAABBUpdater, GeometryUpdaterFactory, GradientType, Group, HTML, Image, LayoutRegistry, Line, LineUpdater, Mask, MutationEvent, Node, OffscreenCanvasCreator, ParseEasingFunction, Path, PathUpdater, Plane, Point, Polygon, Polyline, PolylineUpdater, PropertySyntax, RBushRoot, Rect, RectUpdater, Rectangle, RenderReason, RenderingContext, RenderingPluginContribution, RenderingService, SceneGraphSelector, SceneGraphSelectorFactory, SceneGraphService, Shape, Strategy, StyleValueRegistry, Text, TextService, TextUpdater, UnitType, computeLinearGradient, computeRadialGradient, convertToPath, createVec3, decompose, definedProps, deg2rad, deg2turn, findClosestClipPathTarget, getAngle, getEuler, getOrCalculatePathTotalLength, grad2deg, isBrowser, isFillOrStrokeAffected, isPattern, mergeColors, parseColor, parseLength, parsePath, propertyMetadataCache, rad2deg, setDOMSize, styleValueRegistry, translatePathToString, turn2deg };
19006
+ export { AABB, AbstractRenderer, AbstractRendererPlugin, AnimationTimelineToken, BUILT_IN_PROPERTIES, CSS, CSSGradientValue, CSSKeywordValue, CSSRGB, CSSStyleValue, CSSUnitValue, Camera, CameraContribution, CameraEvent, CameraProjectionMode, CameraTrackingMode, CameraType, Canvas, CanvasConfig, CanvasEvent, Circle, CircleUpdater, ContextService, CustomElement, CustomElementRegistry, CustomEvent, DefaultCamera, DefaultSceneGraphSelector, DefaultSceneGraphService, DisplayObject, DisplayObjectPool, Document, ERROR_MSG_METHOD_NOT_IMPLEMENTED, Element, ElementEvent, Ellipse, EllipseUpdater, EventService, EventTarget, FederatedEvent, FederatedMouseEvent, FederatedPointerEvent, FederatedWheelEvent, Frustum, GeometryAABBUpdater, GeometryUpdaterFactory, GradientType, Group, HTML, Image, LayoutRegistry, Line, LineUpdater, Mask, MutationEvent, Node, OffscreenCanvasCreator, ParseEasingFunction, Path, PathUpdater, Plane, Point, Polygon, Polyline, PolylineUpdater, PropertySyntax, RBushRoot, Rect, RectUpdater, Rectangle, RenderReason, RenderingContext, RenderingPluginContribution, RenderingService, SceneGraphSelector, SceneGraphSelectorFactory, SceneGraphService, Shape, Strategy, StyleValueRegistry, Text, TextService, TextUpdater, UnitType, computeLinearGradient, computeRadialGradient, convertToPath, createVec3, decompose, definedProps, deg2rad, deg2turn, findClosestClipPathTarget, getAngle, getEuler, getOrCalculatePathTotalLength, grad2deg, isBrowser, isFillOrStrokeAffected, isPattern, mergeColors, parseColor, parseLength, parsePath, propertyMetadataCache, rad2deg, resetEntityCounter, setDOMSize, styleValueRegistry, translatePathToString, turn2deg };