spinning_image.dart 2.0 KB
Newer Older
1 2 3 4
// 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.

5
import 'dart:math' as math;
6
import 'dart:sky';
7

A
Adam Barth 已提交
8
import 'package:sky/mojo/net/image_cache.dart' as image_cache;
9 10 11

double timeBase = null;

12
Image image = null;
13 14
String url1 = "https://www.dartlang.org/logos/dart-logo.png";
String url2 = "http://i2.kym-cdn.com/photos/images/facebook/000/581/296/c09.jpg";
15 16

void beginFrame(double timeStamp) {
17 18
  if (timeBase == null)
    timeBase = timeStamp;
19
  double delta = timeStamp - timeBase;
20
  PictureRecorder recorder = new PictureRecorder();
A
Adam Barth 已提交
21
  Canvas canvas = new Canvas(recorder, Point.origin & new Size(view.width, view.height));
22
  canvas.translate(view.width / 2.0, view.height / 2.0);
23
  canvas.rotate(math.PI * delta / 1800);
24
  canvas.scale(0.2, 0.2);
25
  Paint paint = new Paint()..color = const Color.fromARGB(255, 0, 255, 0);
26 27

  // Draw image
28
  if (image != null)
H
Hixie 已提交
29
    canvas.drawImage(image, new Point(-image.width / 2.0, -image.height / 2.0), paint);
30 31 32 33 34 35 36 37 38 39 40 41

  // Draw cut out of image
  canvas.rotate(math.PI * delta / 1800);
  if (image != null) {
    var w = image.width.toDouble();
    var h = image.width.toDouble();
    canvas.drawImageRect(image,
      new Rect.fromLTRB(w * 0.25, h * 0.25, w * 0.75, h * 0.75),
      new Rect.fromLTRB(-w / 4.0, -h / 4.0, w / 4.0, h / 4.0),
      paint);
  }

42
  view.picture = recorder.endRecording();
43 44 45
  view.scheduleFrame();
}

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
void handleImageLoad(result) {
  if (result != image) {
    print("${result.width}x${result.width} image loaded!");
    image = result;
    view.scheduleFrame();
  } else {
    print("Existing image was loaded again");
  }
}

bool handleEvent(Event event) {
  if (event.type == "pointerdown") {
    return true;
  }

  if (event.type == "pointerup") {
62
    image_cache.load(url2).then(handleImageLoad);
63 64 65 66 67 68
    return true;
  }

  return false;
}

69
void main() {
70
  image_cache.load(url1).then(handleImageLoad);
71
  view.setEventCallback(handleEvent);
72
  view.setFrameCallback(beginFrame);
73
}