【问题标题】:How to resize a dynamically printed text in Raphael JS如何在 Raphael JS 中调整动态打印文本的大小
【发布时间】:2012-12-11 02:27:58
【问题描述】:

我已经找到了如何使用 Raphael JS 拖动对象。

但是现在,我想知道是否可以调整动态打印文本的大小并使用 Raphael JS 获取此“对象”的新宽度/高度。

这是我想要完成的第一个视图:

$(function() {
var R = Raphael("holder", 400, 400);
$("#typetext").keyup(function() {
    var textValue = $(this).val();
    R.clear();
    var c = R.print(100, 100, textValue, R.getFont("Arial"), 60).attr({
        fill: "#ff0000",
        cursor: "move"
    });
    var start = function() {
        this.odx = 0;
        this.ody = 0;
        this.animate({
            "fill-opacity": 0.2
        }, 500);
    },
        move = function(dx, dy) {
            this.translate(dx - this.odx, dy - this.ody);
            this.odx = dx;
            this.ody = dy;
        },
        up = function() {
            this.animate({
                "fill-opacity": 1
            }, 500);
        };
    c.drag(move, start, up);
}); 
});​

演示:http://jsfiddle.net/ZuYcG/

任何建议将不胜感激!

编辑:This fiddle 给出了我需要做什么的想法,但带有打印文本。

EDIT2:打印文本由 SVG 结构中的路径定义,似乎无法获得路径的宽度和高度...

【问题讨论】:

    标签: jquery resize raphael


    【解决方案1】:

    不可能“重新打印”,因为将不同的像素高度提供给print 使用的路径生成例程。不过,如果您有兴趣将文本缩放保持在某些合理的边界内,您可以获取路径的边界框,然后对其进行缩放以确保它在画布的宽度和高度范围内。

    var bbox = c.getBBox();
    if ( bbox.x + bbox.width > $(R.canvas).width() )
    {
        var scale = ( $(R.canvas).width() - bbox.x) / bbox.width;
        c.scale( scale, scale, bbox.x, bbox.y  );
    }
    

    功能演示here

    【讨论】:

    • 感谢您的回答凯文。比例影响拖拽速度正常吗?
    • 比例成为对象变换矩阵的持久部分。我认为你可以通过调整你的翻译来补偿缩放,比如this.translate(( dx - this.odx)/scale, (dy - this.ody)/scale)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-17
    • 2010-09-21
    • 2011-12-31
    • 1970-01-01
    • 1970-01-01
    • 2021-01-17
    相关资源
    最近更新 更多