Clean up the standalone examples
Our examples have been growing organically over time. This patch cleans them up to illustrate specific aspects of Flutter.
This commit is contained in:
126
examples/layers/raw/canvas.dart
Normal file
126
examples/layers/raw/canvas.dart
Normal file
@@ -0,0 +1,126 @@
|
||||
// Copyright 2015 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:ui' as ui;
|
||||
import 'dart:math' as math;
|
||||
import 'dart:typed_data';
|
||||
|
||||
ui.Picture paint(ui.Rect paintBounds) {
|
||||
ui.PictureRecorder recorder = new ui.PictureRecorder();
|
||||
ui.Canvas canvas = new ui.Canvas(recorder, paintBounds);
|
||||
ui.Size size = paintBounds.size;
|
||||
|
||||
ui.Paint paint = new ui.Paint();
|
||||
ui.Point mid = size.center(ui.Point.origin);
|
||||
double radius = size.shortestSide / 2.0;
|
||||
canvas.drawPaint(new ui.Paint()..color = const ui.Color(0xFFFFFFFF));
|
||||
|
||||
canvas.save();
|
||||
canvas.translate(-mid.x/2.0, ui.window.size.height*2.0);
|
||||
canvas.clipRect(
|
||||
new ui.Rect.fromLTRB(0.0, -ui.window.size.height, ui.window.size.width, radius));
|
||||
|
||||
canvas.translate(mid.x, mid.y);
|
||||
paint.color = const ui.Color.fromARGB(128, 255, 0, 255);
|
||||
canvas.rotate(math.PI/4.0);
|
||||
|
||||
ui.Gradient yellowBlue = new ui.Gradient.linear(
|
||||
<ui.Point>[new ui.Point(-radius, -radius), new ui.Point(0.0, 0.0)],
|
||||
<ui.Color>[const ui.Color(0xFFFFFF00), const ui.Color(0xFF0000FF)]
|
||||
);
|
||||
canvas.drawRect(new ui.Rect.fromLTRB(-radius, -radius, radius, radius),
|
||||
new ui.Paint()..shader = yellowBlue);
|
||||
|
||||
// Scale x and y by 0.5.
|
||||
Float64List scaleMatrix = new Float64List.fromList(<double>[
|
||||
0.5, 0.0, 0.0, 0.0,
|
||||
0.0, 0.5, 0.0, 0.0,
|
||||
0.0, 0.0, 1.0, 0.0,
|
||||
0.0, 0.0, 0.0, 1.0,
|
||||
]);
|
||||
canvas.transform(scaleMatrix);
|
||||
paint.color = const ui.Color.fromARGB(128, 0, 255, 0);
|
||||
canvas.drawCircle(ui.Point.origin, radius, paint);
|
||||
|
||||
canvas.restore();
|
||||
|
||||
canvas.translate(0.0, 50.0);
|
||||
ui.LayerDrawLooperBuilder builder = new ui.LayerDrawLooperBuilder()
|
||||
..addLayerOnTop(
|
||||
new ui.DrawLooperLayerInfo()
|
||||
..setOffset(const ui.Offset(150.0, 0.0))
|
||||
..setColorMode(ui.TransferMode.src)
|
||||
..setPaintBits(ui.PaintBits.all),
|
||||
new ui.Paint()
|
||||
..color = const ui.Color.fromARGB(128, 255, 255, 0)
|
||||
..colorFilter = new ui.ColorFilter.mode(
|
||||
const ui.Color.fromARGB(128, 0, 0, 255),
|
||||
ui.TransferMode.srcIn
|
||||
)
|
||||
..maskFilter = new ui.MaskFilter.blur(
|
||||
ui.BlurStyle.normal, 3.0, highQuality: true
|
||||
)
|
||||
)
|
||||
..addLayerOnTop(
|
||||
new ui.DrawLooperLayerInfo()
|
||||
..setOffset(const ui.Offset(75.0, 75.0))
|
||||
..setColorMode(ui.TransferMode.src)
|
||||
..setPaintBits(ui.PaintBits.shader),
|
||||
new ui.Paint()
|
||||
..shader = new ui.Gradient.radial(
|
||||
new ui.Point(0.0, 0.0), radius/3.0,
|
||||
<ui.Color>[
|
||||
const ui.Color(0xFFFFFF00),
|
||||
const ui.Color(0xFFFF0000)
|
||||
],
|
||||
null,
|
||||
ui.TileMode.mirror
|
||||
)
|
||||
// Since we're don't set ui.PaintBits.maskFilter, this has no effect.
|
||||
..maskFilter = new ui.MaskFilter.blur(
|
||||
ui.BlurStyle.normal, 50.0, highQuality: true
|
||||
)
|
||||
)
|
||||
..addLayerOnTop(
|
||||
new ui.DrawLooperLayerInfo()..setOffset(const ui.Offset(225.0, 75.0)),
|
||||
// Since this layer uses a DST color mode, this has no effect.
|
||||
new ui.Paint()..color = const ui.Color.fromARGB(128, 255, 0, 0)
|
||||
);
|
||||
paint.drawLooper = builder.build();
|
||||
canvas.drawCircle(ui.Point.origin, radius, paint);
|
||||
|
||||
return recorder.endRecording();
|
||||
}
|
||||
|
||||
ui.Scene composite(ui.Picture picture, ui.Rect paintBounds) {
|
||||
final double devicePixelRatio = ui.window.devicePixelRatio;
|
||||
ui.Rect sceneBounds = new ui.Rect.fromLTWH(
|
||||
0.0,
|
||||
0.0,
|
||||
ui.window.size.width * devicePixelRatio,
|
||||
ui.window.size.height * devicePixelRatio
|
||||
);
|
||||
Float64List deviceTransform = new Float64List(16)
|
||||
..[0] = devicePixelRatio
|
||||
..[5] = devicePixelRatio
|
||||
..[10] = 1.0
|
||||
..[15] = 1.0;
|
||||
ui.SceneBuilder sceneBuilder = new ui.SceneBuilder(sceneBounds)
|
||||
..pushTransform(deviceTransform)
|
||||
..addPicture(ui.Offset.zero, picture)
|
||||
..pop();
|
||||
return sceneBuilder.build();
|
||||
}
|
||||
|
||||
void beginFrame(Duration timeStamp) {
|
||||
ui.Rect paintBounds = ui.Point.origin & ui.window.size;
|
||||
ui.Picture picture = paint(paintBounds);
|
||||
ui.Scene scene = composite(picture, paintBounds);
|
||||
ui.window.render(scene);
|
||||
}
|
||||
|
||||
void main() {
|
||||
ui.window.onBeginFrame = beginFrame;
|
||||
ui.window.scheduleFrame();
|
||||
}
|
||||
79
examples/layers/raw/hello_world.dart
Normal file
79
examples/layers/raw/hello_world.dart
Normal file
@@ -0,0 +1,79 @@
|
||||
// Copyright 2015 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:ui' as ui;
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:mojo/bindings.dart' as bindings;
|
||||
import 'package:mojo/core.dart' as core;
|
||||
import 'package:sky_services/pointer/pointer.mojom.dart';
|
||||
|
||||
ui.Color color;
|
||||
|
||||
ui.Picture paint(ui.Rect paintBounds) {
|
||||
ui.PictureRecorder recorder = new ui.PictureRecorder();
|
||||
ui.Canvas canvas = new ui.Canvas(recorder, paintBounds);
|
||||
ui.Size size = paintBounds.size;
|
||||
|
||||
double radius = size.shortestSide * 0.45;
|
||||
ui.Paint paint = new ui.Paint()..color = color;
|
||||
canvas.drawCircle(size.center(ui.Point.origin), radius, paint);
|
||||
|
||||
return recorder.endRecording();
|
||||
}
|
||||
|
||||
ui.Scene composite(ui.Picture picture, ui.Rect paintBounds) {
|
||||
final double devicePixelRatio = ui.window.devicePixelRatio;
|
||||
ui.Rect sceneBounds = new ui.Rect.fromLTWH(
|
||||
0.0,
|
||||
0.0,
|
||||
ui.window.size.width * devicePixelRatio,
|
||||
ui.window.size.height * devicePixelRatio
|
||||
);
|
||||
Float64List deviceTransform = new Float64List(16)
|
||||
..[0] = devicePixelRatio
|
||||
..[5] = devicePixelRatio
|
||||
..[10] = 1.0
|
||||
..[15] = 1.0;
|
||||
ui.SceneBuilder sceneBuilder = new ui.SceneBuilder(sceneBounds)
|
||||
..pushTransform(deviceTransform)
|
||||
..addPicture(ui.Offset.zero, picture)
|
||||
..pop();
|
||||
return sceneBuilder.build();
|
||||
}
|
||||
|
||||
void beginFrame(Duration timeStamp) {
|
||||
ui.Rect paintBounds = ui.Point.origin & ui.window.size;
|
||||
ui.Picture picture = paint(paintBounds);
|
||||
ui.Scene scene = composite(picture, paintBounds);
|
||||
ui.window.render(scene);
|
||||
}
|
||||
|
||||
void handlePopRoute() {
|
||||
print('Pressed back button.');
|
||||
}
|
||||
|
||||
void handlePointerPacket(ByteData serializedPacket) {
|
||||
bindings.Message message = new bindings.Message(serializedPacket, <core.MojoHandle>[], serializedPacket.lengthInBytes, 0);
|
||||
PointerPacket packet = PointerPacket.deserialize(message);
|
||||
|
||||
for (Pointer pointer in packet.pointers) {
|
||||
if (pointer.type == PointerType.down) {
|
||||
color = new ui.Color.fromARGB(255, 0, 0, 255);
|
||||
ui.window.scheduleFrame();
|
||||
} else if (pointer.type == PointerType.up) {
|
||||
color = new ui.Color.fromARGB(255, 0, 255, 0);
|
||||
ui.window.scheduleFrame();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
print('Hello, world');
|
||||
color = new ui.Color.fromARGB(255, 0, 255, 0);
|
||||
ui.window.onBeginFrame = beginFrame;
|
||||
ui.window.onPopRoute = handlePopRoute;
|
||||
ui.window.onPointerPacket = handlePointerPacket;
|
||||
ui.window.scheduleFrame();
|
||||
}
|
||||
54
examples/layers/raw/spinning_square.dart
Normal file
54
examples/layers/raw/spinning_square.dart
Normal file
@@ -0,0 +1,54 @@
|
||||
// Copyright 2015 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:developer';
|
||||
import 'dart:math' as math;
|
||||
import 'dart:typed_data';
|
||||
import 'dart:ui' as ui;
|
||||
|
||||
Duration timeBase = null;
|
||||
|
||||
void beginFrame(Duration timeStamp) {
|
||||
Timeline.timeSync('beginFrame', () {
|
||||
if (timeBase == null)
|
||||
timeBase = timeStamp;
|
||||
double delta = (timeStamp - timeBase).inMicroseconds / Duration.MICROSECONDS_PER_MILLISECOND;
|
||||
|
||||
// paint
|
||||
ui.Rect paintBounds = ui.Point.origin & ui.window.size;
|
||||
ui.PictureRecorder recorder = new ui.PictureRecorder();
|
||||
ui.Canvas canvas = new ui.Canvas(recorder, paintBounds);
|
||||
canvas.translate(paintBounds.width / 2.0, paintBounds.height / 2.0);
|
||||
canvas.rotate(math.PI * delta / 1800);
|
||||
canvas.drawRect(new ui.Rect.fromLTRB(-100.0, -100.0, 100.0, 100.0),
|
||||
new ui.Paint()..color = const ui.Color.fromARGB(255, 0, 255, 0));
|
||||
ui.Picture picture = recorder.endRecording();
|
||||
|
||||
// composite
|
||||
final double devicePixelRatio = ui.window.devicePixelRatio;
|
||||
ui.Rect sceneBounds = new ui.Rect.fromLTWH(
|
||||
0.0,
|
||||
0.0,
|
||||
ui.window.size.width * devicePixelRatio,
|
||||
ui.window.size.height * devicePixelRatio
|
||||
);
|
||||
Float64List deviceTransform = new Float64List(16)
|
||||
..[0] = devicePixelRatio
|
||||
..[5] = devicePixelRatio
|
||||
..[10] = 1.0
|
||||
..[15] = 1.0;
|
||||
ui.SceneBuilder sceneBuilder = new ui.SceneBuilder(sceneBounds)
|
||||
..pushTransform(deviceTransform)
|
||||
..addPicture(ui.Offset.zero, picture)
|
||||
..pop();
|
||||
ui.window.render(sceneBuilder.build());
|
||||
});
|
||||
|
||||
ui.window.scheduleFrame();
|
||||
}
|
||||
|
||||
void main() {
|
||||
ui.window.onBeginFrame = beginFrame;
|
||||
ui.window.scheduleFrame();
|
||||
}
|
||||
67
examples/layers/raw/text.dart
Normal file
67
examples/layers/raw/text.dart
Normal file
@@ -0,0 +1,67 @@
|
||||
// Copyright 2015 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:ui' as ui;
|
||||
import 'dart:typed_data';
|
||||
|
||||
ui.Paragraph paragraph;
|
||||
|
||||
ui.Picture paint(ui.Rect paintBounds) {
|
||||
ui.PictureRecorder recorder = new ui.PictureRecorder();
|
||||
ui.Canvas canvas = new ui.Canvas(recorder, paintBounds);
|
||||
|
||||
canvas.translate(ui.window.size.width / 2.0, ui.window.size.height / 2.0);
|
||||
canvas.drawRect(new ui.Rect.fromLTRB(-100.0, -100.0, 100.0, 100.0),
|
||||
new ui.Paint()..color = const ui.Color.fromARGB(255, 0, 255, 0));
|
||||
|
||||
canvas.translate(paragraph.maxWidth / -2.0, (paragraph.maxWidth / 2.0) - 125);
|
||||
paragraph.paint(canvas, ui.Offset.zero);
|
||||
|
||||
return recorder.endRecording();
|
||||
}
|
||||
|
||||
ui.Scene composite(ui.Picture picture, ui.Rect paintBounds) {
|
||||
final double devicePixelRatio = ui.window.devicePixelRatio;
|
||||
ui.Rect sceneBounds = new ui.Rect.fromLTWH(
|
||||
0.0,
|
||||
0.0,
|
||||
ui.window.size.width * devicePixelRatio,
|
||||
ui.window.size.height * devicePixelRatio
|
||||
);
|
||||
Float64List deviceTransform = new Float64List(16)
|
||||
..[0] = devicePixelRatio
|
||||
..[5] = devicePixelRatio
|
||||
..[10] = 1.0
|
||||
..[15] = 1.0;
|
||||
ui.SceneBuilder sceneBuilder = new ui.SceneBuilder(sceneBounds)
|
||||
..pushTransform(deviceTransform)
|
||||
..addPicture(ui.Offset.zero, picture)
|
||||
..pop();
|
||||
return sceneBuilder.build();
|
||||
}
|
||||
|
||||
void beginFrame(Duration timeStamp) {
|
||||
ui.Rect paintBounds = ui.Point.origin & ui.window.size;
|
||||
ui.Picture picture = paint(paintBounds);
|
||||
ui.Scene scene = composite(picture, paintBounds);
|
||||
ui.window.render(scene);
|
||||
}
|
||||
|
||||
void main() {
|
||||
ui.ParagraphBuilder builder = new ui.ParagraphBuilder()
|
||||
..pushStyle(new ui.TextStyle(color: const ui.Color(0xFF0000FF)))
|
||||
..addText("Hello, ")
|
||||
..pushStyle(new ui.TextStyle(fontWeight: ui.FontWeight.bold))
|
||||
..addText("world. ")
|
||||
..pop()
|
||||
..addText("هذا هو قليلا طويلة من النص الذي يجب التفاف .")
|
||||
..pop()
|
||||
..addText(" و أكثر قليلا لجعله أطول. ");
|
||||
paragraph = builder.build(new ui.ParagraphStyle())
|
||||
..maxWidth = 180.0
|
||||
..layout();
|
||||
|
||||
ui.window.onBeginFrame = beginFrame;
|
||||
ui.window.scheduleFrame();
|
||||
}
|
||||
Reference in New Issue
Block a user