Repaints and Reflows 重绘和重排版

当浏览器下载完所有页面HTML标记,JavaScript,CSS,图片之后,它解析文件并创建两个内部数据

 一棵DOM树 表示页面结构

一棵渲染树 表示DOM节点如何显示

渲染树中为每个需要显示的DOM树节点存放至少一个节点(隐藏DOM元素在渲染树中没有对应节点)。渲染树上的节点称为“框”或者“盒”,符合CSS模型的定义,将页面元素看作一个具有填充、边距、 边框和位置的盒。一旦DOM树和渲染树构造完毕,浏览器就可以显示(绘制)页面上的元素了

 

当DOM改变影响到元素的几何属性(宽和高)——例如改变了边框宽度或在段落中添加文字,将发生一系列后续动作——浏览器需要重新计算元素的几何属性,而且其他元素的几何属性和位置也会因此改变受到影响。浏览器使渲染树上受到影响的部分失效,然后重构渲染树。这个过程被称作重排版。

重排版完成时,浏览器在一个重绘进程中重新绘制受影响的部分到屏幕中, 该过程称为"重绘"

重绘和重排版是负担很重的操作,可能导致网页应用的用户界面失去相应。所以 尽可能减少这类事情的发生

 

在下述情况中会发生重排版:

添加或删除可见的DOM元素
元素位置改变
元素尺寸改变(因为边距,填充,边框宽度,宽度,高度等属性改变)
内容改变,例如,文本改变或图片被另一个不同尺寸的所替代
最初的页面渲染
浏览器窗口改变尺寸



获取布局信息的操作将导致刷新队列动作,例如:下面这些方法

 offsetTop, offsetLeft, offsetWidth, offsetHeight
• scrollTop, scrollLeft, scrollWidth, scrollHeight
• clientTop, clientLeft, clientWidth, clientHeight
• getComputedStyle() (currentStyle in IE)(在IE中此函数称为currentStyle)
 布局信息由这些属性和方法返回最新的数据,所以浏览器不得不运行渲染队列中待改变的项目并重新排版以返回正确的值

最小化重绘和重排版

1.将所有改变合并在一起执行,只修改DOM一次 例如 定义数组, 把所有动态创建的dom 放入数组 一次加入页面。

2.从文档流中摘除该元素 有三种基本方法可以将DOM从文档中摘除

2.1隐藏元素,进行修改,然后再显示它。

2.2在文档之外创建文档片断
var tempDom = document.createDocumentFragment();
appendDataToElement(
tempDom , data);
document.getElementById('mylist').appendChild(
tempDom );
推荐用文档片断(第二种解决方案)因为它最少数量的DOM操作和重排版。

2.3第三种解决方法首先创建要更新节点的副本,然后在副本上操作,最后用新节点覆盖老节点:
 var old = document.getElementById('mylist');
var clone = old.cloneNode(true);
appendDataToElement(clone, data);
old.parentNode.replaceChild(clone, old);

让元素脱离动画流
使用以下步骤可以避免对大部分页面进行重排版:

1.使用绝对坐标定位页面动画的元素,使它位于页面布局流之外。
2.启动元素动画。当它扩大时,它临时覆盖部分页面。这是一个重绘过程,但只影响页面的一小部分,避免重排版并重绘一大块页面。
3.动画结束时,恢复定位,从而只一次下移文档其他元素的位置。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

当浏览器下载完所有页面HTML标记,JavaScriptCSS,图片之后,它解析文件并创建两个内部数据

结构:

转载于:https://www.cnblogs.com/liufl/p/5621922.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure! Here's a possible implementation of the first stage of the Balloon Pop game in Java: ``` import java.awt.Color; import java.awt.Graphics; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.ArrayList; import java.util.Random; import javax.swing.JFrame; import javax.swing.JPanel; public class BalloonPop extends JPanel implements Runnable { private static final int WIDTH = 500; private static final int HEIGHT = 500; private static final int MAX_RADIUS = 40; private static final int MIN_RADIUS = 20; private static final int GREEN_BALLOON = 0; private static final int RED_BALLOON = 1; private static final int MAX_SPEED = 100; private static final double MIN_TIME = 0.0; private static final double MAX_TIME = 2.0; private ArrayList<Balloon> balloons; private int score; private boolean running; public BalloonPop() { balloons = new ArrayList<Balloon>(); score = 0; running = true; addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { for (int i = 0; i < balloons.size(); i++) { if (balloons.get(i).contains(e.getX(), e.getY())) { if (balloons.get(i).getColor() == GREEN_BALLOON) { score++; } else { score--; } balloons.remove(i); break; } } } }); } public void start() { Thread thread = new Thread(this); thread.start(); } public void stop() { running = false; } @Override public void run() { long startTime = System.currentTimeMillis(); Random random = new Random(); double timeSinceLastBalloon = 0.0; while (running) { long currentTime = System.currentTimeMillis(); double deltaTime = (currentTime - startTime) / 1000.0; double speed = deltaTime / 10.0 + 1.0; double timeBetweenBalloons = random.nextDouble() * (MAX_TIME - MIN_TIME) / speed; if (timeSinceLastBalloon >= timeBetweenBalloons) { int radius = random.nextInt(MAX_RADIUS - MIN_RADIUS + 1) + MIN_RADIUS; int x = random.nextInt(WIDTH - 2 * radius) + radius; int y = -radius; int color = random.nextInt(2); int velocity = random.nextInt(MAX_SPEED + (int)(speed * 50)) + 1; balloons.add(new Balloon(x, y, radius, color, velocity)); timeSinceLastBalloon = 0.0; } else { timeSinceLastBalloon += deltaTime; } for (int i = 0; i < balloons.size(); i++) { balloons.get(i).move(deltaTime); if (balloons.get(i).getY() > HEIGHT + balloons.get(i).getRadius()) { balloons.remove(i); i--; } } repaint(); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.BLACK); g.fillRect(0, 0, WIDTH, HEIGHT); for (int i = 0; i < balloons.size(); i++) { balloons.get(i).draw(g); } g.setColor(Color.WHITE); g.drawString("Score: " + score, 10, 20); } public static void main(String[] args) { JFrame frame = new JFrame("Balloon Pop"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(WIDTH, HEIGHT); frame.setResizable(false); frame.setLocationRelativeTo(null); BalloonPop game = new BalloonPop(); frame.add(game); frame.setVisible(true); game.start(); } } class Balloon { private int x; private int y; private int radius; private int color; private int velocity; public Balloon(int x, int y, int radius, int color, int velocity) { this.x = x; this.y = y; this.radius = radius; this.color = color; this.velocity = velocity; } public void move(double deltaTime) { y += (int)(velocity * deltaTime); } public boolean contains(int x, int y) { int dx = this.x - x; int dy = this.y - y; return dx * dx + dy * dy <= radius * radius; } public void draw(Graphics g) { if (color == BalloonPop.GREEN_BALLOON) { g.setColor(Color.GREEN); } else { g.setColor(Color.RED); } g.fillOval(x - radius, y - radius, 2 * radius, 2 * radius); } public int getX() { return x; } public int getY() { return y; } public int getRadius() { return radius; } public int getColor() { return color; } public int getVelocity() { return velocity; } } ``` This code creates a JPanel that represents the Balloon Pop game. The game window is 500x500 pixels with a black background. Balloons have a random radius of between 20 and 40 pixels and are either green or red. Balloons start with their center at a random x position between their radius and the width of the window minus the radius and a y position of minus the radius. The speed of the game is determined by a variable speed calculated as follows: speed = (time since game has started in seconds) / 10.0 + 1.0. Balloons have a constant random y velocity between 0 and 100 + (speed * 50) pixels per second. The time (in seconds) between balloons appearing is a random double precision number between 0 and 2 divided by the speed. The game logic is implemented in the `run()` method, which generates new balloons, moves existing balloons, updates the score, and repaints the screen. The `Balloon` class represents a single balloon and contains its position, size, color, and velocity. The `contains()` method is used to check if the mouse click is inside a balloon. The `paintComponent()` method is used to draw the balloons and the score on the screen. The `main()` method creates a JFrame and adds the BalloonPop panel to it, then starts the game. I hope that helps! Let me know if you have any further questions.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值