Created
April 19, 2019 19:44
-
-
Save dolpheen/a67e85b6a480cdc430ef7f9cf393fc38 to your computer and use it in GitHub Desktop.
Dart Rotated Box
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:math' as math; | |
import 'dart:typed_data'; | |
import 'dart:ui' as ui; | |
void beginFrame(Duration timeStamp) { | |
// The timeStamp argument to beginFrame indicates the timing information we | |
// should use to clock our animations. It's important to use timeStamp rather | |
// than reading the system time because we want all the parts of the system to | |
// coordinate the timings of their animations. If each component read the | |
// system clock independently, the animations that we processed later would be | |
// slightly ahead of the animations we processed earlier. | |
// PAINT | |
final ui.Rect paintBounds = ui.Offset.zero & (ui.window.physicalSize / ui.window.devicePixelRatio); | |
final ui.PictureRecorder recorder = ui.PictureRecorder(); | |
final ui.Canvas canvas = ui.Canvas(recorder, paintBounds); | |
canvas.translate(paintBounds.width / 2.0, paintBounds.height / 2.0); | |
// Here we determine the rotation according to the timeStamp given to us by | |
// the engine. | |
final double t = timeStamp.inMicroseconds / Duration.microsecondsPerMillisecond / 1800.0; | |
canvas.rotate(math.pi * (t % 1.0) * 3); | |
canvas.drawRect(ui.Rect.fromLTRB(-100.0, -100.0, 100.0, 100.0), | |
ui.Paint()..color = const ui.Color.fromARGB(255, 0, 255, 0)); | |
final ui.Picture picture = recorder.endRecording(); | |
// COMPOSITE | |
final double devicePixelRatio = ui.window.devicePixelRatio; | |
final Float64List deviceTransform = Float64List(16) | |
..[0] = devicePixelRatio | |
..[5] = devicePixelRatio | |
..[10] = 1.0 | |
..[15] = 1.0; | |
final ui.SceneBuilder sceneBuilder = ui.SceneBuilder() | |
..pushTransform(deviceTransform) | |
..addPicture(ui.Offset.zero, picture) | |
..pop(); | |
ui.window.render(sceneBuilder.build()); | |
// After rendering the current frame of the animation, we ask the engine to | |
// schedule another frame. The engine will call beginFrame again when its time | |
// to produce the next frame. | |
ui.window.scheduleFrame(); | |
} | |
void main() { | |
ui.window.onBeginFrame = beginFrame; | |
ui.window.scheduleFrame(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment