Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
triangle.cxx
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_webcanv
3/// User class with custom JavaScript painter in the web canvas.
4///
5/// Custom class is just triangle which drawn on the frame with NDC coordinates
6/// `triangle.mjs` provides JavaScript code for object painting and interactivity
7/// It is also possible to use such "simple" class without loading of custom JS code,
8/// but then it requires appropriate Paint() method and will miss interactivity in browser
9///
10/// This macro must be executed with ACLiC like 'root --web triangle.cxx+'
11///
12/// \macro_image (tcanvas_jsp)
13/// \macro_code
14///
15/// \author Sergey Linev
16
17#include <iostream>
18
19#include "TNamed.h"
20#include "TAttLine.h"
21#include "TAttFill.h"
22#include "TWebCanvas.h"
23#include "TCanvas.h"
24#include "TROOT.h"
25
27
28class TTriangle : public TNamed, public TAttLine, public TAttFill {
29 double fX[3] = {0, 0, 0};
30 double fY[3] = {0, 0, 0};
31
32public:
33 TTriangle() {} // =default not allowed !!!
34
35 TTriangle(const char *name, const char *title = "") : TNamed(name, title) {}
36
37 void SetPoints(double x1, double y1, double x2, double y2, double x3, double y3)
38 {
39 fX[0] = x1;
40 fY[0] = y1;
41 fX[1] = x2;
42 fY[1] = y2;
43 fX[2] = x3;
44 fY[2] = y3;
45 }
46
47 /** In old graphics just provide line drawing */
48 void Paint(Option_t *opt) override
49 {
50 if (!gPad)
51 return;
52 TAttLine::Modify(); // Change line attributes only if necessary
53 TAttFill::Modify(); // Change fill area attributes only if necessary
54
55 if (*opt == 'f')
56 gPad->PaintFillAreaNDC(3, fX, fY, opt);
57 else
58 gPad->PaintPolyLineNDC(3, fX, fY, opt);
59 }
60
61 ClassDefOverride(TTriangle, 1); // Example of triangle drawing in web canvas
62};
63
64void triangle(bool ignore_jsmodule = false)
65{
66 if (ignore_jsmodule) {
67 printf("Custom JS module will NOT be provided for TTriangle class\n");
68 printf("No interactive features will be available\n");
69 printf("TTriangle::Paint() method will be used for object painting - also in web case\n");
70 } else {
71#ifdef __CLING__
72 printf("Please run this script in compiled mode by running \".x triangle.cxx+\"\n");
73 printf("Requires to properly generate dictionary for TTriangle class\n");
74 return;
75#endif
76
77 std::string fdir = __FILE__;
78 auto pos = fdir.find("triangle.cxx");
79 if (pos != std::string::npos)
80 fdir.resize(pos);
81 else
82 fdir = gROOT->GetTutorialsDir() + std::string("/webcanv/");
83
84 // location required to load files
85 // also it is name of modules path used in importmap
86 ROOT::RWebWindowsManager::AddServerLocation("tutorials_webcanv", fdir);
87
88 // mark TTriangle as supported on the client
89 TWebCanvas::AddCustomClass("TTriangle");
90
91 // specify which extra module should be loaded,
92 // "tutorials_webcanv/" is registered path from server locations
93 TWebCanvas::SetCustomScripts("modules:tutorials_webcanv/triangle.mjs");
94 }
95
96 auto tr1 = new TTriangle("tr1", "first triangle");
97 tr1->SetPoints(0.4, 0.5, 0.8, 0.3, 0.8, 0.7);
98 tr1->SetLineColor(kRed);
99 tr1->SetLineWidth(3);
100 tr1->SetFillColor(kBlue);
101
102 auto tr2 = new TTriangle("tr2", "second triangle");
103 tr2->SetPoints(0.2, 0.2, 0.2, 0.6, 0.5, 0.4);
104 tr2->SetLineColor(kGreen);
105 tr2->SetLineStyle(kDashed);
106 tr2->SetFillColor(kYellow);
107
108 auto tr3 = new TTriangle("tr3", "third triangle");
109 tr3->SetPoints(0.4, 0.8, 0.6, 0.8, 0.5, 0.2);
110 tr3->SetLineColor(kCyan);
111 tr3->SetFillColor(kMagenta);
112
113 auto c1 = new TCanvas("c1", "Triangles");
114 c1->Add(tr1, "f");
115 c1->Add(tr2, "f");
116 c1->Add(tr3, "f");
117
118 // test image saving with web browser, chrome or firefox are required
119 // c1->SaveAs("triangle.png");
120 // c1->SaveAs("triangle.pdf");
121}
const char Option_t
Definition RtypesCore.h:66
@ kRed
Definition Rtypes.h:66
@ kGreen
Definition Rtypes.h:66
@ kMagenta
Definition Rtypes.h:66
@ kCyan
Definition Rtypes.h:66
@ kBlue
Definition Rtypes.h:66
@ kYellow
Definition Rtypes.h:66
#define ClassDefOverride(name, id)
Definition Rtypes.h:346
@ kDashed
Definition TAttLine.h:52
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char x2
Option_t Option_t TPoint TPoint const char x1
Option_t Option_t TPoint TPoint const char y2
Option_t Option_t TPoint TPoint const char y1
char name[80]
Definition TGX11.cxx:110
#define gROOT
Definition TROOT.h:406
#define gPad
static void AddServerLocation(const std::string &server_prefix, const std::string &files_path)
Configure server location which can be used for loading of custom scripts or files When THttpServer i...
Fill Area Attributes class.
Definition TAttFill.h:20
virtual void Modify()
Change current fill area attributes if necessary.
Definition TAttFill.cxx:216
Line Attributes class.
Definition TAttLine.h:20
virtual void Modify()
Change current line attributes if necessary.
Definition TAttLine.cxx:247
The Canvas class.
Definition TCanvas.h:23
The TNamed class is the base class for all named ROOT classes.
Definition TNamed.h:29
static void AddCustomClass(const std::string &clname, bool with_derived=false)
Assign custom class.
static void SetCustomScripts(const std::string &src)
Configures custom script for canvas.
return c1
Definition legend1.C:41