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 8import 'dart:math' as math; 9import 'dart:typed_data'; 10import 'dart:ui' as ui; 11 12void 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 22 final ui.Rect paintBounds = ui.Offset.zero & (ui.window.physicalSize / ui.window.devicePixelRatio); 23 final ui.PictureRecorder recorder = ui.PictureRecorder(); 24 final ui.Canvas canvas = ui.Canvas(recorder, paintBounds); 25 canvas.translate(paintBounds.width / 2.0, paintBounds.height / 2.0); 26 27 // Here we determine the rotation according to the timeStamp given to us by 28 // the engine. 29 final double t = timeStamp.inMicroseconds / Duration.microsecondsPerMillisecond / 1800.0; 30 canvas.rotate(math.pi * (t % 1.0)); 31 32 canvas.drawRect(const ui.Rect.fromLTRB(-100.0, -100.0, 100.0, 100.0), 33 ui.Paint()..color = const ui.Color.fromARGB(255, 0, 255, 0)); 34 final ui.Picture picture = recorder.endRecording(); 35 36 // COMPOSITE 37 38 final double devicePixelRatio = ui.window.devicePixelRatio; 39 final Float64List deviceTransform = Float64List(16) 40 ..[0] = devicePixelRatio 41 ..[5] = devicePixelRatio 42 ..[10] = 1.0 43 ..[15] = 1.0; 44 final ui.SceneBuilder sceneBuilder = ui.SceneBuilder() 45 ..pushTransform(deviceTransform) 46 ..addPicture(ui.Offset.zero, picture) 47 ..pop(); 48 ui.window.render(sceneBuilder.build()); 49 50 // After rendering the current frame of the animation, we ask the engine to 51 // schedule another frame. The engine will call beginFrame again when its time 52 // to produce the next frame. 53 ui.window.scheduleFrame(); 54} 55 56void main() { 57 ui.window.onBeginFrame = beginFrame; 58 ui.window.scheduleFrame(); 59} 60