Skip to content

Instantly share code, notes, and snippets.

@roipeker
Created November 26, 2020 22:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roipeker/f5987e7158a3a7e932e2547e3d919951 to your computer and use it in GitHub Desktop.
Save roipeker/f5987e7158a3a7e932e2547e3d919951 to your computer and use it in GitHub Desktop.
/// copyright roipeker 2020
///
/// web demo:
/// https://roi-graphx-spiral3d.surge.sh
///
import 'package:flutter/material.dart';
import 'package:graphx/graphx.dart';
main() {
runApp(MaterialApp(
home: SceneBuilderWidget(
builder: () => SceneController(back: DnaScene()),
),
));
}
class DnaScene extends Sprite {
double spiralRadius, centerX, centerY, centerZ;
double fl = 150;
List<Dot> dots;
@override
void addedToStage() async {
stage.color = Colors.white.value;
dots = List.generate(80, (index) {
var dot = Dot();
addChild(dot);
dot.py = -200.0 + index * 5.0;
dot.angle = index * 20.0;
return dot;
});
}
@override
void update(t) {
centerX = stage.stageWidth / 2;
centerY = stage.stageHeight / 2;
centerZ = 100.0;
spiralRadius = (mouseX - centerX) / 4;
dots.forEach((dot) {
double dotAngle = deg2rad(dot.angle);
var pz = sin(dotAngle) * spiralRadius + centerZ;
var fov = fl / (fl + pz);
/// calculate `px` position only, `py` is predefined in the
/// initialization.
var px = cos(dotAngle) * spiralRadius;
dot.x = px * fov + centerX;
dot.y = dot.py * fov + centerY;
dot.alpha = dot.scale = fov;
dot.updateAngle();
});
}
}
/// we just make a class to store the basic properties
/// for the calculations.
class Dot extends Shape {
double py, angle, speed = 5;
Dot() {
graphics.beginFill(0x0).drawCircle(0, 0, 10).endFill();
}
void updateAngle() {
angle += speed;
if (angle >= 360) {
angle -= 360;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment