Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
triangle.cxx File Reference

Detailed Description

User class with custom JavaScript painter in the web canvas.

Custom class is just triangle which drawn on the frame with NDC coordinates triangle.mjs provides JavaScript code for object painting and interactivity It is also possible to use such "simple" class without loading of custom JS code, but then it requires appropriate Paint() method and will miss interactivity in browser

This macro must be executed with ACLiC like 'root –web triangle.cxx+'

#include <iostream>
#include "TNamed.h"
#include "TAttLine.h"
#include "TAttFill.h"
#include "TWebCanvas.h"
#include "TCanvas.h"
#include "TROOT.h"
class TTriangle : public TNamed, public TAttLine, public TAttFill {
double fX[3] = {0, 0, 0};
double fY[3] = {0, 0, 0};
public:
TTriangle() {} // =default not allowed !!!
TTriangle(const char *name, const char *title = "") : TNamed(name, title) {}
void SetPoints(double x1, double y1, double x2, double y2, double x3, double y3)
{
fX[0] = x1;
fY[0] = y1;
fX[1] = x2;
fY[1] = y2;
fX[2] = x3;
fY[2] = y3;
}
/** In old graphics just provide line drawing */
void Paint(Option_t *opt) override
{
if (!gPad)
return;
TAttLine::Modify(); // Change line attributes only if necessary
TAttFill::Modify(); // Change fill area attributes only if necessary
if (*opt == 'f')
gPad->PaintFillAreaNDC(3, fX, fY, opt);
else
gPad->PaintPolyLineNDC(3, fX, fY, opt);
}
ClassDefOverride(TTriangle, 1); // Example of triangle drawing in web canvas
};
void triangle(bool ignore_jsmodule = false)
{
if (ignore_jsmodule) {
printf("Custom JS module will NOT be provided for TTriangle class\n");
printf("No interactive features will be available\n");
printf("TTriangle::Paint() method will be used for object painting - also in web case\n");
} else {
#ifdef __CLING__
printf("Please run this script in compiled mode by running \".x triangle.cxx+\"\n");
printf("Requires to properly generate dictionary for TTriangle class\n");
return;
#endif
std::string fdir = __FILE__;
auto pos = fdir.find("triangle.cxx");
if (pos != std::string::npos)
fdir.resize(pos);
else
fdir = gROOT->GetTutorialsDir() + std::string("/webcanv/");
// location required to load files
// also it is name of modules path used in importmap
ROOT::RWebWindowsManager::AddServerLocation("tutorials_webcanv", fdir);
// mark TTriangle as supported on the client
// specify which extra module should be loaded,
// "tutorials_webcanv/" is registered path from server locations
TWebCanvas::SetCustomScripts("modules:tutorials_webcanv/triangle.mjs");
}
auto tr1 = new TTriangle("tr1", "first triangle");
tr1->SetPoints(0.4, 0.5, 0.8, 0.3, 0.8, 0.7);
tr1->SetLineColor(kRed);
tr1->SetLineWidth(3);
tr1->SetFillColor(kBlue);
auto tr2 = new TTriangle("tr2", "second triangle");
tr2->SetPoints(0.2, 0.2, 0.2, 0.6, 0.5, 0.4);
tr2->SetLineColor(kGreen);
tr2->SetLineStyle(kDashed);
tr2->SetFillColor(kYellow);
auto tr3 = new TTriangle("tr3", "third triangle");
tr3->SetPoints(0.4, 0.8, 0.6, 0.8, 0.5, 0.2);
tr3->SetLineColor(kCyan);
tr3->SetFillColor(kMagenta);
auto c1 = new TCanvas("c1", "Triangles");
c1->Add(tr1, "f");
c1->Add(tr2, "f");
c1->Add(tr3, "f");
// test image saving with web browser, chrome or firefox are required
// c1->SaveAs("triangle.png");
// c1->SaveAs("triangle.pdf");
}
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
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
virtual void Paint(Option_t *option="")
This method must be overridden if a class wants to paint itself.
Definition TObject.cxx:624
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
Author
Sergey Linev

Definition in file triangle.cxx.