|
| 1 | +// Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +// This example shows how to perform a simple animation using the raw interface |
| 6 | +// to the engine. |
| 7 | + |
| 8 | +import 'dart:math' as math; |
| 9 | +import 'dart:typed_data'; |
| 10 | +import 'dart:ui' as ui; |
| 11 | + |
| 12 | +void beginFrame(Duration timeStamp) { |
| 13 | + // The timeStamp argument to beginFrame indicates the timing information we |
| 14 | + // should use to clock our animations. It's important to use timeStamp rather |
| 15 | + // than reading the system time because we want all the parts of the system to |
| 16 | + // coordinate the timings of their animations. If each component read the |
| 17 | + // system clock independently, the animations that we processed later would be |
| 18 | + // slightly ahead of the animations we processed earlier. |
| 19 | + |
| 20 | + // PAINT |
| 21 | + const double kRadius = 100.0; |
| 22 | + const double kTwoPi = math.PI * 2.0; |
| 23 | + const double kVerticalOffset = 100.0; |
| 24 | + |
| 25 | + final ui.Rect paintBounds = |
| 26 | + ui.Offset.zero & (ui.window.physicalSize / ui.window.devicePixelRatio); |
| 27 | + final ui.PictureRecorder recorder = new ui.PictureRecorder(); |
| 28 | + final ui.Canvas canvas = new ui.Canvas(recorder, paintBounds); |
| 29 | + canvas.translate(paintBounds.width / 2.0, paintBounds.height / 2.0); |
| 30 | + |
| 31 | + // Here we determine the rotation speed according to the timeStamp given to us |
| 32 | + // by the engine. |
| 33 | + final double t = (timeStamp.inMicroseconds / |
| 34 | + Duration.MICROSECONDS_PER_MILLISECOND / |
| 35 | + 3200.0) % 1.0; |
| 36 | + |
| 37 | + final List<ui.Offset> points = <ui.Offset>[ |
| 38 | + const ui.Offset(kRadius, kVerticalOffset), |
| 39 | + const ui.Offset(0.0, kVerticalOffset), |
| 40 | + new ui.Offset(kRadius * math.cos(t * kTwoPi), |
| 41 | + kRadius * math.sin(t * kTwoPi) + kVerticalOffset), |
| 42 | + ]; |
| 43 | + |
| 44 | + // Try changing values for the stroke style and see what the results are |
| 45 | + // for different line drawing primitives. |
| 46 | + final ui.Paint paint = new ui.Paint() |
| 47 | + ..color = const ui.Color.fromARGB(255, 0, 255, 0) |
| 48 | + ..style = ui.PaintingStyle.stroke |
| 49 | + ..strokeCap = ui.StrokeCap.butt // Other choices are round and square. |
| 50 | + ..strokeJoin = ui.StrokeJoin.miter // Other choices are round and bevel. |
| 51 | + ..strokeMiterLimit = 5.0 // Try smaller and larger values greater than zero. |
| 52 | + ..strokeWidth = 20.0; |
| 53 | + canvas.drawPoints(ui.PointMode.polygon, points, paint); |
| 54 | + |
| 55 | + final ui.Path path = new ui.Path() |
| 56 | + ..moveTo(points[0].dx, points[0].dy - 2 * kVerticalOffset) |
| 57 | + ..lineTo(points[1].dx, points[1].dy - 2 * kVerticalOffset) |
| 58 | + ..lineTo(points[2].dx, points[2].dy - 2 * kVerticalOffset) |
| 59 | + ..close(); |
| 60 | + canvas.drawPath(path, paint); |
| 61 | + final ui.Picture picture = recorder.endRecording(); |
| 62 | + |
| 63 | + // COMPOSITE |
| 64 | + |
| 65 | + final double devicePixelRatio = ui.window.devicePixelRatio; |
| 66 | + final Float64List deviceTransform = new Float64List(16) |
| 67 | + ..[0] = devicePixelRatio |
| 68 | + ..[5] = devicePixelRatio |
| 69 | + ..[10] = 1.0 |
| 70 | + ..[15] = 1.0; |
| 71 | + final ui.SceneBuilder sceneBuilder = new ui.SceneBuilder() |
| 72 | + ..pushTransform(deviceTransform) |
| 73 | + ..addPicture(ui.Offset.zero, picture) |
| 74 | + ..pop(); |
| 75 | + ui.window.render(sceneBuilder.build()); |
| 76 | + |
| 77 | + // After rendering the current frame of the animation, we ask the engine to |
| 78 | + // schedule another frame. The engine will call beginFrame again when its time |
| 79 | + // to produce the next frame. |
| 80 | + ui.window.scheduleFrame(); |
| 81 | +} |
| 82 | + |
| 83 | +void main() { |
| 84 | + ui.window.onBeginFrame = beginFrame; |
| 85 | + ui.window.scheduleFrame(); |
| 86 | +} |
0 commit comments