From d65f230e0fe7d3230336a2331ed74645019255ff Mon Sep 17 00:00:00 2001 From: mkwiser Date: Sun, 21 Sep 2014 15:16:32 +0800 Subject: [PATCH] fix paste fix graduation --- src/editor/controller/initRender.js | 19 +++++++++++- src/render/render.js | 3 +- src/render/util/drawGraduation.js | 46 +++++++++++++++++++++-------- 3 files changed, 54 insertions(+), 14 deletions(-) diff --git a/src/editor/controller/initRender.js b/src/editor/controller/initRender.js index bc859b6..07f2711 100644 --- a/src/editor/controller/initRender.js +++ b/src/editor/controller/initRender.js @@ -12,7 +12,9 @@ define( var commandList = require('../menu/commandList'); var lang = require('common/lang'); - + var modeSupport = require('../mode/support'); + var pathAdjust = require('graphics/pathAdjust'); + var computeBoundingBox = require('graphics/computeBoundingBox'); /** * 右键点击处理 @@ -26,6 +28,21 @@ define( else if (e.command == 'paste') { var shapes = this.getClipBoard(); if(shapes) { + var bound = computeBoundingBox.computePath.apply(null, + shapes.map(function(shape){ + return shape.points; + }) + ); + // 需要根据坐标原点以及缩放换算成鼠标位置移动 + var origin = this.axis; + var pos = e.pos; + var scale = this.render.camera.scale; + var x = (pos.x - origin.x) / scale; + var y = (origin.y - pos.y) / scale; + + shapes.forEach(function(shape) { + pathAdjust(shape.points, 1, 1, x - bound.x, y - bound.y - bound.height); + }); this.setShapes(shapes); this.setMode('shapes', shapes); this.fire('change'); diff --git a/src/render/render.js b/src/render/render.js index dd4ecc9..a33b712 100644 --- a/src/render/render.js +++ b/src/render/render.js @@ -57,8 +57,9 @@ define( ) { return; } - + console.time('refresh'); me.scale(ratio, e); + console.timeEnd('refresh'); }); } } diff --git a/src/render/util/drawGraduation.js b/src/render/util/drawGraduation.js index 7eb802f..df3f360 100644 --- a/src/render/util/drawGraduation.js +++ b/src/render/util/drawGraduation.js @@ -18,6 +18,7 @@ define( */ function drawGraduation(ctx, config) { + var x = Math.round(config.x); var y = Math.round(config.y); @@ -68,22 +69,16 @@ define( ctx.fillStyle = ctx.strokeStyle; + // 横轴线 - var textOffset = thickness - 8; // 文本偏移 for(var axis = x, i = 0; axis < width; i++, axis += markSize) { ctx.moveTo(axis, thickness - (i % 5 ? markHeight : 2 * markHeight)); ctx.lineTo(axis, thickness); - if (0 == i % 10) { - ctx.fillText( gap * i, axis, textOffset); - } } for(var axis = x, i = 0; axis > thickness; i++, axis -= markSize) { ctx.moveTo(axis, thickness - (i % 5 ? markHeight : 2 * markHeight)); ctx.lineTo(axis, thickness); - if (0 == i % 10) { - ctx.fillText( -gap * i, axis, textOffset); - } } @@ -92,19 +87,46 @@ define( for(var axis = y, i = 0; axis > thickness; i++, axis -= markSize) { ctx.moveTo(thickness - (i % 5 ? markHeight : 2 * markHeight), axis); ctx.lineTo(thickness, axis); - if (0 == i % 10) { - ctx.fillText(gap * i, textOffset, axis); - } } for(var axis = y, i = 0; axis < height; i++, axis += markSize) { ctx.moveTo(thickness - (i % 5 ? markHeight : 2 * markHeight), axis); ctx.lineTo(thickness, axis); + } + + // 绘制轴线文字,这里由于canvas不支持小字体,因此进行缩放后绘制 + ctx.scale(0.8, 0.8); + var textOffset = thickness - 8; // 文本偏移 + for(var axis = x, i = 0; axis < width; i++, axis += markSize) { if (0 == i % 10) { - ctx.fillText( -gap * i, 0, axis); + ctx.fillText( gap * i, axis * 1.25 - 3, textOffset * 1.25); } } - + + for(var axis = x, i = 0; axis > thickness; i++, axis -= markSize) { + if (0 == i % 10) { + ctx.fillText( -gap * i, axis * 1.25 - 3, textOffset * 1.25); + } + } + + + // 纵轴线 + var textOffset = 0; // 文本偏移 + for(var axis = y, i = 0; axis > thickness; i++, axis -= markSize) { + if (0 == i % 10) { + ctx.fillText(gap * i, textOffset * 1.25, axis * 1.25 + 3); + } + } + + for(var axis = y, i = 0; axis < height; i++, axis += markSize) { + if (0 == i % 10) { + ctx.fillText( -gap * i, textOffset * 1.25, axis * 1.25 + 3); + } + } + + ctx.scale(1.25, 1.25); + + ctx.stroke(); }