安卓控件大小动态文字排版

Lately I’ve been interested in creating motion typography for the web. Making shattered text turned out to be a combination of SVG, JavaScript and CSS, which I thought might make a useful tutorial article.

最近,我对创建Web的动作排版感兴趣。 制作破碎的文本原来是SVG , JavaScript和CSS的组合,我认为这可能会成为有用的教程文章。

制作文字 (Making The Text)

Neither HTML nor CSS has the ability to fragment letterforms: that’s currently the sole province of SVG. To make that happen, I created text in Adobe Illustrator, broke the letters into outlines, and split the result using the Knife tool, working from the inside out. This created “breakpoints” in the text, while maintaining the appearance of wholeness.

HTML和CSS都不具有对字母格式进行分段的功能:这是SVG的唯一省份。 为此,我在Adobe Illustrator中创建了文本,将字母分解为轮廓,然后使用“ 切刀”工具从内到外拆分结果。 这在文本中创建了“断点”,同时保持了整体外观。

Text broken into fragments with Adobe Illustrator
使用Adobe Illustrator将文本分成碎片

The result, exported as an SVG and given a quick code cleanup, is a series of simple paths:

作为SVG导出并给出快速代码清除的结果是一系列简单的路径:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 475.7 162.3" id="heavy"><path d="M72, 134.1v0.9h30.5v-8.8C92, 127.2, 81.9, 130.4, 72, 134.1z"/><path d="M102.6, 113c-10.1, 1.5-20.3, 2.2-30.5, 2.7v18.4c9.9-3.7, 20-6.9, 30.5-7.8V113z" />
…

This SVG code is placed directly in the body of an HTML document.

该SVG代码直接放置在HTML文档的主体中。

By default SVG paths are filled with black, but I wanted the reverse. I also needed to add a stroke definition, so that the SVG pieces looked seamless. The following CSS will achieve that:

默认情况下,SVG路径填充为黑色,但我想反过来。 我还需要添加stroke定义,以便SVG片段看起来无缝。 以下CSS将实现这一目标:

body { background: #000; }
svg#heavy path { fill: white; stroke: white; }

With the fractures in place, I needed to consider how to make the pieces fly apart.

骨折到位后,我需要考虑如何使碎片飞散。

运动设计 (Motion Design)

Animating each of the letter fragments by hand would be incredibly tedious; instead, I decided to write the motion as a script. Before starting to code, I had to design what the motion would look like, and then translate it into mathematics.

手工制作每个字母片段的动画将非常繁琐; 相反,我决定将议案作为脚本来编写。 在开始编码之前,我必须设计运动的外观,然后将其转化为数学形式。

From the separation lines I used to divide the letters, I knew that the word “HEAVY” would explode outwards from its center. This means that every piece would travel in a radial line from the center of the text. If I could locate each piece, then I could determine its distance (x and y) from the center. Moving each piece could then be a simple matter of taking a portion of x and y and using a CSS transform to translate it further in the same direction.

从我用来分隔字母的分隔线中,我知道“ HEAVY”一词将从其中心向外爆炸。 这意味着每一段都将从文本中心沿径向线行进。 如果我可以找到每个零件,则可以确定其与中心的距离( x和y )。 那么移动每个片段就很简单,只需取x和y的一部分,然后使用CSS变换将其沿相同方向进一步平移即可。

Illustration of a fragment path as it moves outwards after initial explosion
初始爆炸后碎片路径向外移动的示意图

使用JavaScript编写动作脚本 (Scripting Motion With JavaScript)

A script at the end of the document gains the information we need we need to create the motion.  We can’t use the standard offsetLeft to locate SVG elements; instead, we have to use getBBox():

文档末尾的脚本获得了创建动作所需的信息。 我们不能使用标准的offsetLeft来定位SVG元素; 相反,我们必须使用getBBox()

var heavy = document.getElementById("heavy"),
dim = heavy.getBBox(),
heavyCenterX = dim.width/2,
heavyCenterY = dim.height/2,
force = 8;
heavyPieces = document.querySelectorAll("svg#heavy path");

Next, we’ll loop though the paths:

接下来,我们将遍历路径:

for (var i=0; i < heavyPieces.length; i++) {var piece = heavyPieces[i];piece.id = "fragment"+i;var bbox = piece.getBBox();var pieceCenterX = bbox.x + (bbox.width/2);var pieceCenterY = bbox.y + (bbox.height/2);var distanceX = Math.abs(heavyCenterX - pieceCenterX);var distanceY = Math.abs(heavyCenterY - pieceCenterY);…
}

Each fragment is given an id, with a calculated center and a distance from the center of the text.

每个片段都有一个id ,其id带有计算得出的中心以及距文本中心的距离。

Next, we move the pieces. If the fragment is in the top right corner, it moves up and to the right; if it’s in the lower left corner, it moves to the left and down. At the same time, we want to ensure that each fragment moves proportional to its position: the inner fragments by just a fraction, and the outer ones significantly more. I’ll use force to reduce the distances proportionally:

接下来,我们移动碎片。 如果片段在右上角,则它将向上和向右移动; 如果它位于左下角,则它将向左和向下移动。 同时,我们要确保每个片段都按其位置成比例地移动:内部片段仅占一小部分,而外部片段则更多。 我将force按比例减小距离:

if (pieceCenterX > heavyCenterX) {var moveX = distanceX/force+"px";
} else {var moveX = "-"+distanceX/force+"px"; }
if (pieceCenterY > heavyCenterY) {var moveY = distanceY/force+"px";
} else {var moveY = "-"+distanceY/force+"px";
}

It will be a little boring if the fragments only move in straight lines away from the center; I’ll also add a little random spin:

如果片段仅沿直线远离中心移动,将有些无聊; 我还将添加一些随机旋转:

var force = 8,
min = -2.5,
max = 2.5,
randomRot = Math.floor(Math.random() * (max - min + 1)) + min;

I want this action to take place when the user hovers over the text. Putting all of this together in a transform applied with JavaScript, and ignoring vendor prefixes for the sake of simplicity:

我希望用户将鼠标悬停在文本上时执行此操作。 将所有这些放在一起进行JavaScript转换,并为简单起见忽略供应商前缀 :

document.styleSheets[0].insertRule("svg:hover #fragment"+i+" { transform: translate("+moveX+","+moveY+") rotate("+randomRot+"deg) }",1);

This is good, but the text doesn’t actually animate yet: a hover over the SVG will simply place the fragments in their final position.

很好,但是文本实际上还没有动画:将鼠标悬停在SVG上只会将片段放置在最终位置。

创建动作电影动作 (Creating Action Movie Motion)

Figure 1: Bezier curve for the motion of explosion fragments
图1:爆炸碎片运动的贝塞尔曲线

I wanted a very specific kind of motion for the explosion: a “move quick, then slow motion” movement familiar from modern action movies. After a little fiddling in Ceaser, I came up with a motion graph that looks like Figure 1.

我想要一种非常特殊的爆炸动作:现代动作电影中熟悉的“先快后慢”动作。 在对Ceaser稍作摆弄之后 ,我想出了一个类似于图1的运动图。

Time is measured horizontally in this graph, with “amount of change” vertically: so what you see is a very quick “explosion” phase, followed by a long, drawn-out movement driven by the initial impetus.

在此图中,时间是水平测量的,垂直是“变化量”的:因此,您看到的是一个非常快速的“爆炸”阶段,随后是由初始动力驱动的长而缓慢的运动。

As a CSS Bezier curve timing function, it looks something like this:

作为CSS Bezier曲线计时功能,它看起来像这样:

svg#heavy path {fill: white;stroke: white;transition: 12s 1.6s cubic-bezier(0, 1, 0, 1);
}

Note the relatively long duration (12 seconds) and the brief pause beforehand (1.6 seconds). The pause is present because I needed some motion to initially break up the letters, which is the next step.

请注意相对较长的持续时间(12秒)和短暂的预先暂停(1.6秒)。 之所以会出现暂停,是因为我需要一些动作来初步分解字母,这是下一步。

在压力之下 (Under Pressure)

Without a cue – the sound of a gunshot or a visual impact – there’s no reason for the letters to fly apart in the first place. I decided to animate the word “HEAVY” as a whole, as it was as struggling to contain internal pressure.

没有提示(枪声或视觉冲击声),字母就没有理由飞散了。 我决定对整个“ HEAVY”这个词进行动画处理,因为它难以承受内部压力。

While it would be possible to write this in JavaScript too, I decided to simply code it a CSS keyframe animation:

尽管也可以用JavaScript编写此代码,但我还是决定将其编码为CSS关键帧动画:

@keyframes shake {0% { transform: translate(3px,5px); }5% { transform: translate(8px,-5px); }10% { transform: translate(-3px,2px); }…
}

The motion doesn’t have to be much, but it does need to move relatively quickly:

动作不必太多,但确实需要相对快速地移动:

svg:hover { animation: shake 1s linear; }

With the timing delay on the explosion, the animation and transitions work together to give the impression of a seamless sequence.

由于爆炸的时间延迟,动画和过渡效果共同发挥作用,给人以无缝序列的印象。

添加触控支持 (Adding Touch Support)

To gain support on iOS and other devices I created a quick hack to initiate the same behaviour on touch:

为了获得对iOS和其他设备的支持,我创建了一个快速技巧来启动触摸时的相同行为:

var paths = document.getElementsByTagName('path')[0];
heavy.onclick = function() {
paths.onhover.call(paths);
};

结论 (Conclusion)

While the result works well, there are a few improvements that could be made… the most significant being auto-fragmenting the text using JavaScript, rather than drawing the divisions by hand. This is a challenge I will leave for a future article.

虽然效果很好,但是可以进行一些改进……最重要的是使用JavaScript自动对文本进行片段化,而不是手工绘制分割。 这是我将在以后的文章中提出的挑战。

翻译自: https://thenewcode.com/868/Dynamic-Web-Typography-Exploding-Text

安卓控件大小动态文字排版

安卓控件大小动态文字排版_动态Web排版:爆炸文字相关推荐

  1. android 动态设置padding,Android动态设置控件大小以及设定margin以及padding值

    http://www.aichengxu.com/Java/73893.htm Android动态设置控件大小以及设定margin以及padding值,有需要的朋友可以参考下. 一.概述 在andro ...

  2. python 动态调整控件大小_python GUI库图形界面开发之PyQt5动态(可拖动控件大小)布局控件QSplitter详细使用方法与实例...

    PyQt5动态(可拖动控件大小)布局控件QSplitter简介 PyQt还提供了特殊的布局管理器QSplitter.它可以动态地拖动子控件之间的边界,算是一个动态的布局管理器,QSplitter允许用 ...

  3. pyqt5中控件缩放功能_python GUI库图形界面开发之PyQt5动态(可拖动控件大小)布局控件QSplitter详细使用方法与实例...

    PyQt5动态(可拖动控件大小)布局控件QSplitter简介 PyQt还提供了特殊的布局管理器QSplitter.它可以动态地拖动子控件之间的边界,算是一个动态的布局管理器,QSplitter允许用 ...

  4. 新一代纯前端控件集 WijmoJS 2018V2发布,提供 Web 设计器,可动态设计页面并生成代码...

    概述 作为一款纯前端控件集,WijmoJS 秉承"快如闪电,触控优先"的设计理念,在提供优质服务和产品的同时,专注于企业应用开发,不断优化产品架构,与时俱进.除在全球率先支持 An ...

  5. C#如何实现窗体控件大小随窗体大小变化(包括字体)

    如图,拖动窗体即可改变控件大小(包括字体) 窗体尺寸:345*315 窗体尺寸:603*509 项目资源如下 源码如下 using System; using System.Collections.G ...

  6. COM组件开发实践(七)---多线程ActiveX控件和自动调整ActiveX控件大小(上)

    声明:本文代码基于CodeProject的文章<A Complete ActiveX Web Control Tutorial>修改而来,因此同样遵循Code Project Open L ...

  7. COM组件开发实践(八)---多线程ActiveX控件和自动调整ActiveX控件大小(下)

    源代码下载:MyActiveX20081229.rar 声明:本文代码基于CodeProject的文章<A Complete ActiveX Web Control Tutorial>修改 ...

  8. 在vc2005中使用MoveWindow()调整控件大小,不能及时刷新,在vc6中则可以

    在vc2005中使用MoveWindow()调整控件大小,不能及时刷新,在vc6中则可以? // ------------------------------ vc2005 ------------- ...

  9. 用了GradientDrawable后,当点击控件时,控件大小发生变化

    android新手:发现一个很奇怪的问题,用了GradientDrawable后,当点击控件时,程序自动使我的一些控件大小保持一致,为什么呢,我就是不想它们保持一致啊 改了好久好久:GradientD ...

最新文章

  1. es6笔记2之解构赋值,rest和展开运算符
  2. 递推DP UVA 590 Always on the run
  3. Linux之du df free:du文件大小 df分区使用 free内存
  4. P1351-联合权值【树形结构】
  5. android 事件反拦截
  6. 差异表达基因变化倍数_2020年筛个基因也还能发SCI?
  7. 「三分钟系列06」3分钟看懂http与https的区别
  8. 《Node应用程序构建——使用MongoDB和Backbone》一2.3 事件
  9. OpenCV读取网络摄像头视频并保存到本地
  10. 滚动字幕制作C语言示例
  11. Qt数据可视化(QPieSeries饼状图)
  12. RHCE认证考试成绩公布(转)
  13. PyQT5 (四十三) 在 QTableWidget 表格中设置单元格的字体和颜色 的案例
  14. 【阅读】阅读软件Calibre以及电子书下载地址
  15. 笔记本电脑如何用c语言开无线网卡,笔记本电脑无线上网卡怎么用 笔记本电脑无线上网卡使用方法...
  16. 贪吃的猴子c语言的思路,贪吃的猴子(贪吃的猴子?C语言)
  17. 【无标题】翻译数字孪生经典文章Shaping the digital twin for design and production engineering
  18. u-collapse折叠面板双层for循环渲染无法加载内层
  19. 去广外读计算机类还是去广大,广财会计学专业很受考生青睐
  20. KISSsoft 2022 SP3

热门文章

  1. kubernetes系列之二十:Kubernetes Calico网络插件
  2. 织梦tag伪静态html方式,织梦dedecms TAG标签伪静态方法(附伪静态规则)
  3. 手把手教你Centos 7.6 的安装部署
  4. 罗技M545鼠标失灵起死回生
  5. 虚拟蜜罐Honeyd的分析和研究
  6. vue3全局挂载组件
  7. WordPress上传错误等问题
  8. HTML中元素标签的分类及结构标签
  9. “大数据” 监管建筑市场
  10. 展示制作的发票报销系统项目