Skip to content

Instantly share code, notes, and snippets.

@marc-hughes
Created January 2, 2014 17:50
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 marc-hughes/8223144 to your computer and use it in GitHub Desktop.
Save marc-hughes/8223144 to your computer and use it in GitHub Desktop.
Dynamically drawing on a canvas with an angular.dart component.
<div class="game_container" >
<game-timeline></game-timeline>
</div>
.timeline-container {
width: 100%;
position: fixed;
bottom: 0px;
left: 50%;
margin-left: -400px;
}
library timelineview;
import 'dart:html';
import 'package:angular/angular.dart';
@NgComponent(
selector: 'game-timeline',
templateUrl: '/static/dart/game/web/views/timelineview.html',
cssUrl: '/static/dart/game/web/views/timelineview.css',
publishAs: 'ctrl'
)
class TimelineView extends Object with NgShadowRootAware {
int maxDays = 50;
int _currentDay = 10;
CanvasRenderingContext2D context;
void set currentDay(int val) {
_currentDay = val;
drawTimeline();
}
int get currentDay {
return _currentDay;
}
void onShadowRoot(ShadowRoot shadowRoot) {
CanvasElement canvas = shadowRoot.querySelector("#timelineStage");
initCanvas(canvas);
drawTimeline();
}
void initCanvas(CanvasElement canvas) {
context = canvas.context2D;
}
void drawTimeline() {
context.clearRect(0, 0, 800, 600);
context.save();
CanvasGradient gradient = context.createLinearGradient(0, 0, 0, 60);
gradient.addColorStop(0, '#333');
gradient.addColorStop(1, '#000');
context.fillStyle = gradient;
context.fillRect(0,0,805,40);
for( var i = 0 ; i < maxDays ; i++) {
context.lineWidth = 2;
context.setStrokeColorRgb(120, 120, 120,1);
if( i != (maxDays-1)) {
context.beginPath();
context.moveTo(15+i*16,20);
context.lineTo(20+i*16,20);
context.stroke();
}
if( i < currentDay ) {
context.setStrokeColorRgb(255, 255, 255,1);
} else {
context.setStrokeColorRgb(120, 120, 120,1);
}
context.beginPath();
context.ellipse(10+i*16, 20, 5, 5, 0, 0, 360, false);
context.stroke();
}
context.save();
}
}
<div class="timeline-container">
<canvas id="timelineStage" width="805" height="40"></canvas>
</div
@mmcc007
Copy link

mmcc007 commented Jun 10, 2018

The following works for Angular 5.0 running on Dart 2.0:
https://gist.github.com/mmcc007/f289f69fb63f3a142fda03dc60fd8f89

Tested on following versions:
environment:
sdk: '>=2.0.0-dev.55.0 <2.0.0'
dependencies:
angular: ^5.0.0-alpha+13
angular_components: ^0.9.0-alpha+13

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment