修改变换矩阵

This commit is contained in:
mkwiser
2014-09-13 13:29:52 +08:00
parent ee7f73f86f
commit e8b9418265
4 changed files with 102 additions and 93 deletions

View File

@@ -163,7 +163,7 @@ define(
*/
Editor.prototype.setFont = function(font) {
var paths = glyf2path(font);
var contours = font.contours;
var width = this.render.painter.width;
var height = this.render.painter.height;
@@ -174,13 +174,11 @@ define(
var offsetY = (height + (options.unitsPerEm + options.metrics.WinDecent)) / 2;
// 构造形状集合
var shapes = paths.map(function(path) {
var shapes = contours.map(function(path) {
var shape = {};
var bound = computeBoundingBox.computePath(path);
path = pathAdjust(path, 1, -1);
shape.points = pathAdjust(path, 1, 1, offsetX, offsetY);
return shape;
});

View File

@@ -14,6 +14,7 @@ define(
var boundAdjust = require('render/util/boundAdjust');
var lang = require('common/lang');
var computeBoundingBox = require('../../graphics/computeBoundingBox');
var getTransformMatrix = require('../util/getTransformMatrix');
/**
* 获取bound边界
@@ -94,70 +95,10 @@ define(
*/
Group.prototype.transform = function(point, camera) {
var bound = this.bound;
// x, y, xscale 相对符号, yscale 相对符号
var matrix = [
0,
0,
1,
1
];
// 是否需要等比例缩放
var ctrlKey = camera.event && camera.event.ctrlKey;
switch (point.pos) {
case 1:
matrix[0] = bound.x + bound.width;
matrix[1] = bound.y + bound.height;
matrix[2] = -(camera.x - matrix[0]) / bound.width;
matrix[3] = -(camera.y - matrix[1]) / bound.height;
break;
case 2:
matrix[0] = bound.x;
matrix[1] = bound.y + bound.height;
matrix[2] = (camera.x - matrix[0]) / bound.width;
matrix[3] = -(camera.y - matrix[1]) / bound.height;
break;
case 3:
matrix[0] = bound.x;
matrix[1] = bound.y;
matrix[2] = (camera.x - matrix[0]) / bound.width;
matrix[3] = (camera.y - matrix[1]) / bound.height;
break;
case 4:
matrix[0] = bound.x + bound.width;
matrix[1] = bound.y;
matrix[2] = -(camera.x - matrix[0]) / bound.width;
matrix[3] = (camera.y - matrix[1]) / bound.height;
break;
case 5:
matrix[1] = bound.y + bound.height;
matrix[2] = 1;
matrix[3] = -(camera.y - matrix[1]) / bound.height;
break;
case 7:
matrix[1] = bound.y;
matrix[3] = (camera.y - matrix[1]) / bound.height;
break;
case 6:
matrix[0] = bound.x;
matrix[2] = (camera.x - matrix[0]) / bound.width;
break;
case 8:
matrix[0] = bound.x + bound.width;
matrix[2] = -(camera.x - matrix[0]) / bound.width;
break;
};
var matrix = getTransformMatrix(point.pos, this.bound, camera);
// 等比缩放
if (camera.event.ctrlKey && [1, 2, 3, 4].indexOf(point.pos) >= 0) {
if (camera.event.shiftKey && [1, 2, 3, 4].indexOf(point.pos) >= 0) {
var scale = Math.max(Math.abs(matrix[2]), Math.abs(matrix[3]));
matrix[2] = matrix[2] >= 0 ? scale : -scale;
matrix[3] = matrix[3] >= 0 ? scale : -scale;

View File

@@ -10,7 +10,6 @@
define(
function(require) {
var pathIterator = require('render/util/pathIterator');
var computeBoundingBox = require('graphics/computeBoundingBox');
var pathAdjust = require('render/util/pathAdjust');
var lang = require('common/lang');
@@ -85,32 +84,15 @@ define(
var shapes = this.render.getLayer('font').shapes;
shapes.forEach(function(shape) {
pathIterator(shape.points, function(c, i, p0, p1, p2) {
if(c == 'M' || c == 'L') {
controls.push({
type: 'point',
x: p0.x,
y: p0.y,
_point: p0,
_shape: shape.id
});
}
else if (c == 'Q') {
controls.push({
type: 'cpoint',
x: p1.x,
y: p1.y,
_point: p1,
_shape: shape.id
});
controls.push({
type: 'point',
x: p2.x,
y: p2.y,
_point: p2,
_shape: shape.id
});
}
shape.points.forEach(function(p) {
controls.push({
type: p.onCurve ? 'point' : 'cpoint',
x: p.x,
y: p.y,
_point: p,
_shape: shape.id
});
});
});

View File

@@ -0,0 +1,88 @@
/**
* @file getTransformMatrix.js
* @author mengke01
* @date
* @description
* 获得变换的矩阵
*/
define(
function(require) {
/**
* 获得变换矩阵
*
* @param {number} pos 变换位置
* @param {Object} bound 边界
* @param {Object} camera 镜头对象
* @return {Array} 变换矩阵x,y,xScale,yScale
*/
function getTransformMatrix(pos, bound, camera) {
// x, y, xscale 相对符号, yscale 相对符号
var matrix = [
0,
0,
1,
1
];
switch (pos) {
case 1:
matrix[0] = bound.x + bound.width;
matrix[1] = bound.y + bound.height;
matrix[2] = -(camera.x - matrix[0]) / bound.width;
matrix[3] = -(camera.y - matrix[1]) / bound.height;
break;
case 2:
matrix[0] = bound.x;
matrix[1] = bound.y + bound.height;
matrix[2] = (camera.x - matrix[0]) / bound.width;
matrix[3] = -(camera.y - matrix[1]) / bound.height;
break;
case 3:
matrix[0] = bound.x;
matrix[1] = bound.y;
matrix[2] = (camera.x - matrix[0]) / bound.width;
matrix[3] = (camera.y - matrix[1]) / bound.height;
break;
case 4:
matrix[0] = bound.x + bound.width;
matrix[1] = bound.y;
matrix[2] = -(camera.x - matrix[0]) / bound.width;
matrix[3] = (camera.y - matrix[1]) / bound.height;
break;
case 5:
matrix[1] = bound.y + bound.height;
matrix[2] = 1;
matrix[3] = -(camera.y - matrix[1]) / bound.height;
break;
case 7:
matrix[1] = bound.y;
matrix[3] = (camera.y - matrix[1]) / bound.height;
break;
case 6:
matrix[0] = bound.x;
matrix[2] = (camera.x - matrix[0]) / bound.width;
break;
case 8:
matrix[0] = bound.x + bound.width;
matrix[2] = -(camera.x - matrix[0]) / bound.width;
break;
};
return matrix;
}
return getTransformMatrix;
}
);