Last active
August 29, 2015 13:57
-
-
Save CaptainJH/9871621 to your computer and use it in GitHub Desktop.
some cairo code snippet in cinder
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
// draw window background | |
// before this ctx should be created first in every rendering frame, like this: | |
// cairo::Context ctx(cario::createWindowSurface()) | |
void renderScene(cairo::Contect& ctx) | |
{ | |
// clear the context with our radial gradient | |
cairo::GradientRadial radialGrad( getWindowCenter(), 0, getWindowCenter(), getWindowWidth() ); | |
radialGrad.addColorStop( 0, Color( 1, 1, 1 ) ); | |
radialGrad.addColorStop( 1, Color( 0.6, 0.6, 0.6 ) ); | |
ctx.setSource( radialGrad ); | |
ctx.paint(); | |
} | |
void drawCircle(cinder::cairo::Context& ctx) | |
{ | |
ColorA color(CM_RGB, 0.0f, 0.0f, 0.0f, 0.65f); | |
ctx.setSource(color); | |
ctx.newPath(); | |
ctx.circle(location, 10); | |
ctx.fill(); | |
ctx.closePath(); | |
} | |
void drawTriangle(cinder::cairo::Context& ctx, cinder::Vec2f pos, float length) | |
{ | |
ColorA color(CM_RGB, 0.0f, 0.0f, 0.0f, 0.65f); | |
ctx.setSource(color); | |
ctx.newPath(); | |
{ | |
const float squareRoot3 = sqrt(3.0f); | |
cinder::Vec2f b(pos.x - length / 2, pos.y + length / (2 * squareRoot3)); | |
cinder::Vec2f c(pos.x + length / 2, pos.y + length / (2 * squareRoot3)); | |
cinder::Vec2f a(pos.x, pos.y - length); | |
ctx.line(a, b); | |
ctx.line(b, c); | |
ctx.line(c, a); | |
} | |
ctx.stroke(); | |
ctx.closePath(); | |
} | |
void rotateContext() | |
{ | |
ctx.save(); | |
ctx.translate(location); // first, move context origin to location where you want to draw | |
ctx.rotate(m_rotation); // rotate | |
cinder::ColorA color(CM_RGB, m_isPushing ? 1.0f : 0.0f, 0.0f, 0.0f, 0.65f); // always draw at (0.0f, 0.0f) | |
ctx.setSource(color); | |
ctx.newPath(); | |
makeTrianglePathAtPos(Vec2f(0.0f, 0.0f), 30, ctx); | |
ctx.stroke(); | |
ctx.closePath(); | |
ctx.restore(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment