首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在PaperJS中,允许用户像常规文本输入字段一样编辑TextItem吗?

问在PaperJS中,允许用户像常规文本输入字段一样编辑TextItem吗?
EN

Stack Overflow用户
提问于 2015-12-27 20:21:12
回答 1查看 1.8K关注 0票数 0

我正在使用PaperJS制作一个帆布应用程序,在每个气球中生成带有文本的气球。但是,我希望允许用户编辑每个气球内的文本,以便他们想说什么就说什么。

是否允许用户编辑PaperJS TextItem,就像HTML输入字段一样?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-28 04:09:52

简单地说,答案是否定的,除非您从零开始实现并行功能。我所使用的解决方案是让用户绘制一个矩形,然后使用绝对定位将矩形覆盖在画布上的文本框或文本区域相同的位置。它需要额外的抽象级别,但可以很好地工作。

这并不简单,但这里有一个基本的框架,展示了它的工作原理。我可能会有时间让它在线,但这将需要一点,所以我不知道什么时候。我也在从一个更大的系统中快速提取这个信息,所以如果你发现任何错误,请告诉我。

代码语言:javascript
复制
var rect;
var tool = new paper.Tool();

// create a paper rectangle. it's just a visual indicator of where the
// text will go.
tool.onMouseDown = function(e) {
    rect = new paper.Path.Rectangle(
               from: e.downPoint,
               to: e.downPoint,
               strokeColor: 'red',
               );
}

tool.onMouseDrag = function(3) {
    if (rect) {
        rect.remove();
    }
    rect = new paper.path.Rectangle({
            from: e.downPoint,
            to: e.point,
            strokeColor: 'red'
            });
}

tool.onMouseUp = function(e) {
    var bounds = rect.bounds;
    var textarea = $("<textarea class='dynamic-textarea' " + 
                   "style='position:absolute; left:" + bounds.x +
                   "px; top:" + bounds.y + "px; width: " + bounds.width +
                   "px; height: " + bounds.height +
                   "px; resize;' placeholder='Enter text'></textarea>");
    // make the paper rectangle invisible for now. may want to show on
    // mouseover or when selected.
    rect.visible = false;

    // add the text area to the DOM then remember it in the path
    $("#parent-div").append(textarea);
    rect.data.textarea = textarea;

    // you may want to give the textarea focus, assign tab indexes, etc.
};
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34484447

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档