2017-07-08 05:55:23 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using Ooui;
|
|
|
|
|
|
|
|
namespace Samples
|
|
|
|
{
|
2017-11-10 06:35:47 +00:00
|
|
|
public class DrawSample : ISample
|
2017-07-08 05:55:23 +00:00
|
|
|
{
|
2017-11-16 04:32:17 +00:00
|
|
|
public string Title => "Drawing";
|
2017-11-10 06:35:47 +00:00
|
|
|
|
2017-07-08 05:55:23 +00:00
|
|
|
public void Publish ()
|
2017-11-10 06:35:47 +00:00
|
|
|
{
|
|
|
|
UI.Publish ("/draw", CreateElement ());
|
|
|
|
}
|
|
|
|
|
|
|
|
public Element CreateElement ()
|
2017-07-08 05:55:23 +00:00
|
|
|
{
|
|
|
|
var heading = new Heading ("Draw");
|
2017-11-16 05:03:36 +00:00
|
|
|
var subtitle = new Paragraph ("Click to draw a masterpiece");
|
2017-12-09 21:20:55 +00:00
|
|
|
var toolSel = new Select ();
|
|
|
|
toolSel.AppendChild (new Option { Label = "Boxes", Value = "box" });
|
|
|
|
toolSel.AddOption ("Circles", "circle");
|
2017-07-08 05:55:23 +00:00
|
|
|
var canvas = new Canvas {
|
|
|
|
Width = 320,
|
|
|
|
Height = 240,
|
|
|
|
};
|
|
|
|
var context = canvas.GetContext2D ();
|
|
|
|
|
|
|
|
canvas.Clicked += (s, e) => {
|
2017-12-09 21:20:55 +00:00
|
|
|
var radius = 10;
|
2017-07-08 05:55:23 +00:00
|
|
|
context.BeginPath ();
|
2017-12-09 21:20:55 +00:00
|
|
|
if (toolSel.Value == "box") {
|
|
|
|
context.Rect (e.OffsetX - radius, e.OffsetY - radius, 2*radius, 2*radius);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
context.Arc (e.OffsetX, e.OffsetY, radius, 0, 2 * Math.PI, true);
|
|
|
|
}
|
2017-07-08 05:55:23 +00:00
|
|
|
context.Fill ();
|
|
|
|
};
|
|
|
|
canvas.Style.Cursor = "pointer";
|
|
|
|
canvas.Style.BorderColor = "#CCC";
|
|
|
|
canvas.Style.BorderStyle = "solid";
|
|
|
|
canvas.Style.BorderWidth = "1px";
|
|
|
|
|
|
|
|
var clearbtn = new Button ("Clear") {
|
|
|
|
Type = ButtonType.Submit,
|
|
|
|
ClassName = "btn btn-danger",
|
|
|
|
};
|
|
|
|
clearbtn.Clicked += (s, e) => {
|
|
|
|
context.ClearRect (0, 0, canvas.Width, canvas.Height);
|
|
|
|
};
|
|
|
|
clearbtn.Style.Display = "block";
|
|
|
|
|
|
|
|
var app = new Div ();
|
|
|
|
app.AppendChild (heading);
|
|
|
|
app.AppendChild (subtitle);
|
2017-12-09 21:20:55 +00:00
|
|
|
app.AppendChild (new Div (toolSel));
|
2017-07-08 05:55:23 +00:00
|
|
|
app.AppendChild (canvas);
|
|
|
|
app.AppendChild (clearbtn);
|
2017-11-10 06:35:47 +00:00
|
|
|
return app;
|
2017-07-08 05:55:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|