I am, trying to create a simple animation sequence using Raphael.js. Specifically I have a set of raphael elements (circles) in an array and need them to fade in one by one.
I found a jQuery plugin "jQuery.eachStep()" but I can't figure out how to get it to work. The Rapheal is working, but the "eachStep" is not.
var rsr = Raphael('rsr', '600', '300');
var fillColor = {fill: '#00A651','stroke-width': '0','stroke-opacity': '0'}
var circle_b = rsr.circle(513, 128, 4).attr(fillColor);
var circle_c = rsr.circle(477, 128, 4).attr(fillColor);
var circle_d = rsr.circle(441, 128, 4).attr(fillColor);
var circle_e = rsr.circle(406, 128, 4).attr(fillColor);
var circle_f = rsr.circle(370, 128, 4).attr(fillColor);
var circle_g = rsr.circle(334, 128, 4).attr(fillColor);
var circle_h = rsr.circle(298, 128, 4).attr(fillColor);
var circle_i = rsr.circle(262, 128, 4).attr(fillColor);
var circle_j = rsr.circle(226, 128, 4).attr(fillColor);
var circle_k = rsr.circle(191, 128, 4).attr(fillColor);
var circle_l = rsr.circle(155, 128, 4).attr(fillColor);
var collection = [
circle_b, circle_c, circle_d, circle_e, circle_f, circle_g, circle_H, circle_i, circle_j, circle_k, circle_l
];
$.eachStep(collection, '600', function(i, val, duration){
val.animate({'opacity': '0'}, duration, 'linear');
});
Or if I could get a proper for loop working that would be sufficient.
http://jsfiddle.net/s5vAL/3/
I think you probably don't want an each function unless you increment a delay or something in it. The animate method has a callback which will call a function on completion of the animation (the 4th parameter). So you can make it recursive like this...
var rsr = Raphael('rsr', '600', '300');
var fillColor = {fill: '#00A651','stroke-width': '0','opacity': '1'}
var circle_b = rsr.circle(513, 128, 4).attr(fillColor);
var circle_c = rsr.circle(477, 128, 4).attr(fillColor);
var circle_d = rsr.circle(441, 128, 4).attr(fillColor);
var circle_e = rsr.circle(406, 128, 4).attr(fillColor);
var circle_f = rsr.circle(370, 128, 4).attr(fillColor);
var circle_g = rsr.circle(334, 128, 4).attr(fillColor);
var circle_h = rsr.circle(298, 128, 4).attr(fillColor);
var circle_i = rsr.circle(262, 128, 4).attr(fillColor);
var circle_j = rsr.circle(226, 128, 4).attr(fillColor);
var circle_k = rsr.circle(191, 128, 4).attr(fillColor);
var circle_l = rsr.circle(155, 128, 4).attr(fillColor);
var collection = [
circle_b, circle_c, circle_d, circle_e, circle_f, circle_g, circle_h, circle_i, circle_j, circle_k, circle_l
];
function nextAnim( count ) {
if( count >= collection.length ) { return }
collection[ count ].animate( {'opacity': '0'}, 1000, 'linear', nextAnim.bind(null,count+1 ) );
};
nextAnim(0);
jsfiddle here
Related
I'm kind of a noob, sorry if the answer is obvious but apparently nobody has asked that same question before.
I'm in the process of writing an article and I'm already using several other Amcharts charts in the article webpage (that are working just fine) like piecharts and sorted bar charts. Now, my problem is that my 100% stacked column charts seem to be working just fine on the plug-in preview mode but just never show up on the webpage, leaving a blank space instead.
This is really weird as that problem exists only with this type of charts, while the other ones appear and load just fine.
I've already given specific IDs to all charts and I made sure to give unique names to variables as well so that there are no duplicates that might screw everything up. Really I am clueless as to what the cause of that problem is and I need some help. Thank you in advance to whomever might be willing to assist me.
Here is the code for both 100% stacked column charts:
Chart 1
var chart10 = am4core.create("chartdiv10", am4charts.XYChart);
chart10.hiddenState.properties.opacity = 0; // this creates initial fade-in
chart10.data = [
{
category: "Matériaux utilisés",
value1: 5,
value2: 5,
value3: 5,
value4: 23,
value5: 39,
value6: 28,
value7: 24
},
{
category: "Marque",
value1: 6,
value2: 9,
value3: 5,
value4: 15,
value5: 39,
value6: 36,
value7: 19
},
{
category: "Possibilités de personnalisation",
value1: 19,
value2: 19,
value3: 28,
value4: 24,
value5: 12,
value6: 15,
value7: 12
},
{
category: "Label Swiss Made",
value1: 7,
value2: 9,
value3: 11,
value4: 23,
value5: 23,
value6: 17,
value7: 39
},
{
category: "Possibilités de revente",
value1: 38,
value2: 23,
value3: 16,
value4: 15,
value5: 16,
value6: 10,
value7: 11
},
{
category: "Ecoresponsabilité",
value1: 25,
value2: 20,
value3: 20,
value4: 21,
value5: 19,
value6: 9,
value7: 15
},
{
category: "Certifications officielles",
value1: 8,
value2: 6,
value3: 6,
value4: 16,
value5: 23,
value6: 33,
value7: 37
},
{
category: "Montre neuve",
value1: 13,
value2: 13,
value3: 6,
value4: 14,
value5: 18,
value6: 25,
value7: 40
},
]
chart10.padding(30, 30, 10, 30);
chart10.colors.list = [
am4core.color("#81BEF7"),
am4core.color("#58ACFA"),
am4core.color("#2E9AFE"),
am4core.color("#0080FF"),
am4core.color("#2E64FE"),
am4core.color("#0431B4"),
am4core.color("#0B2161")
];
var categoryAxis10 = chart10.xAxes.push(new am4charts.CategoryAxis());
categoryAxis10.dataFields.category = "category";
categoryAxis10.renderer.grid.template.location = 0;
var valueAxis10 = chart10.yAxes.push(new am4charts.ValueAxis());
valueAxis10.min = 0;
valueAxis10.max = 100;
valueAxis10.strictMinMax = true;
valueAxis10.calculateTotals = true;
valueAxis10.renderer.minWidth = 40;
var series110 = chart10.series.push(new am4charts.ColumnSeries());
series110.columns.template.width = am4core.percent(80);
series110.columns.template.tooltipText =
"{name}: {valueY.totalPercent.formatNumber('#.00')}%";
series110.name = "1";
series110.dataFields.categoryX = "category";
series110.dataFields.valueY = "value1";
series110.dataFields.valueYShow = "totalPercent";
series110.dataItems.template.locations.categoryX = 0.5;
series110.stacked = true;
series110.tooltip.pointerOrientation = "vertical";
var bullet110 = series110.bullets.push(new am4charts.LabelBullet());
bullet110.interactionsEnabled = false;
bullet110.label.text = "1";
bullet110.label.fill = am4core.color("#ffffff");
bullet110.locationY = 0.5;
var series210 = chart10.series.push(new am4charts.ColumnSeries());
series210.columns.template.width = am4core.percent(80);
series210.columns.template.tooltipText =
"{name}: {valueY.totalPercent.formatNumber('#.00')}%";
series210.name = "2";
series210.dataFields.categoryX = "category";
series210.dataFields.valueY = "value2";
series210.dataFields.valueYShow = "totalPercent";
series210.dataItems.template.locations.categoryX = 0.5;
series210.stacked = true;
series210.tooltip.pointerOrientation = "vertical";
var bullet210 = series210.bullets.push(new am4charts.LabelBullet());
bullet210.interactionsEnabled = false;
bullet210.label.text = "2";
bullet210.label.fill = am4core.color("#ffffff");
bullet210.locationY = 0.5;
var series310 = chart10.series.push(new am4charts.ColumnSeries());
series310.columns.template.width = am4core.percent(80);
series310.columns.template.tooltipText =
"{name}: {valueY.totalPercent.formatNumber('#.00')}%";
series310.name = "3";
series310.dataFields.categoryX = "category";
series310.dataFields.valueY = "value3";
series310.dataFields.valueYShow = "totalPercent";
series310.dataItems.template.locations.categoryX = 0.5;
series310.stacked = true;
series310.tooltip.pointerOrientation = "vertical";
var bullet310 = series310.bullets.push(new am4charts.LabelBullet());
bullet310.interactionsEnabled = false;
bullet310.label.text = "3";
bullet310.locationY = 0.5;
bullet310.label.fill = am4core.color("#ffffff");
var series410 = chart10.series.push(new am4charts.ColumnSeries());
series410.columns.template.width = am4core.percent(80);
series410.columns.template.tooltipText =
"{name}: {valueY.totalPercent.formatNumber('#.00')}%";
series410.name = "4";
series410.dataFields.categoryX = "category";
series410.dataFields.valueY = "value4";
series410.dataFields.valueYShow = "totalPercent";
series410.dataItems.template.locations.categoryX = 0.5;
series410.stacked = true;
series410.tooltip.pointerOrientation = "vertical";
var bullet410 = series410.bullets.push(new am4charts.LabelBullet());
bullet410.interactionsEnabled = false;
bullet410.label.text = "4";
bullet410.label.fill = am4core.color("#ffffff");
bullet410.locationY = 0.5;
var series510 = chart10.series.push(new am4charts.ColumnSeries());
series510.columns.template.width = am4core.percent(80);
series510.columns.template.tooltipText =
"{name}: {valueY.totalPercent.formatNumber('#.00')}%";
series510.name = "5";
series510.dataFields.categoryX = "category";
series510.dataFields.valueY = "value5";
series510.dataFields.valueYShow = "totalPercent";
series510.dataItems.template.locations.categoryX = 0.5;
series510.stacked = true;
series510.tooltip.pointerOrientation = "vertical";
var bullet510 = series510.bullets.push(new am4charts.LabelBullet());
bullet510.interactionsEnabled = false;
bullet510.label.text = "5";
bullet510.label.fill = am4core.color("#ffffff");
bullet510.locationY = 0.5;
var series610 = chart10.series.push(new am4charts.ColumnSeries());
series610.columns.template.width = am4core.percent(80);
series610.columns.template.tooltipText =
"{name}: {valueY.totalPercent.formatNumber('#.00')}%";
series610.name = "6";
series610.dataFields.categoryX = "category";
series610.dataFields.valueY = "value6";
series610.dataFields.valueYShow = "totalPercent";
series610.dataItems.template.locations.categoryX = 0.5;
series610.stacked = true;
series610.tooltip.pointerOrientation = "vertical";
var bullet610 = series610.bullets.push(new am4charts.LabelBullet());
bullet610.interactionsEnabled = false;
bullet610.label.text = "6";
bullet610.label.fill = am4core.color("#ffffff");
bullet610.locationY = 0.5;
var series710 = chart10.series.push(new am4charts.ColumnSeries());
series710.columns.template.width = am4core.percent(80);
series710.columns.template.tooltipText =
"{name}: {valueY.totalPercent.formatNumber('#.00')}%";
series710.name = "7";
series710.dataFields.categoryX = "category";
series710.dataFields.valueY = "value7";
series710.dataFields.valueYShow = "totalPercent";
series710.dataItems.template.locations.categoryX = 0.5;
series710.stacked = true;
series710.tooltip.pointerOrientation = "vertical";
var bullet710 = series710.bullets.push(new am4charts.LabelBullet());
bullet710.interactionsEnabled = false;
bullet710.label.text = "7";
bullet710.label.fill = am4core.color("#ffffff");
bullet710.locationY = 0.5;
categoryAxis10.renderer.labels.template.rotation = 320;
Chart 2
var chart89 = am4core.create("chartdiv89", am4charts.XYChart);
chart89.hiddenState.properties.opacity = 0; // this creates initial fade-in
chart89.data = [
{
category89: "Innovante",
value189: 4,
value289: 9,
value389: 34,
value489: 52,
value589: 33
},
{
category89: "Moderne",
value189: 4,
value289: 15,
value389: 44,
value489: 44,
value589: 25
},
{
category89: "Proche de ses clients",
value189: 10,
value289: 6,
value389: 34,
value489: 41,
value589: 41
},
{
category89: "Fermée/exclusive",
value189: 3,
value289: 8,
value389: 32,
value489: 33,
value589: 56
},
{
category89: "Attrayante professionnellement",
value289: 5,
value389: 15,
value489: 44,
value589: 68
}]
chart89.padding(30, 30, 10, 30);
chart89.colors.list = [
am4core.color("#2E9AFE"),
am4core.color("#0080FF"),
am4core.color("#2E64FE"),
am4core.color("#0431B4"),
am4core.color("#0B2161"),
];
var categoryAxis89 = chart89.xAxes.push(new am4charts.CategoryAxis());
categoryAxis89.dataFields.category = "category89";
categoryAxis89.renderer.grid.template.location = 0;
var valueAxis89 = chart89.yAxes.push(new am4charts.ValueAxis());
valueAxis89.min = 0;
valueAxis89.max = 100;
valueAxis89.strictMinMax = true;
valueAxis89.calculateTotals = true;
valueAxis89.renderer.minWidth = 40;
var series189 = chart89.series.push(new am4charts.ColumnSeries());
series189.columns.template.width = am4core.percent(80);
series189.columns.template.tooltipText =
"{name}: {valueY.totalPercent.formatNumber('#.00')}%";
series189.name = "1";
series189.dataFields.categoryX = "category89";
series189.dataFields.valueY = "value189";
series189.dataFields.valueYShow = "totalPercent";
series189.dataItems.template.locations.categoryX = 0.5;
series189.stacked = true;
series189.tooltip.pointerOrientation = "vertical";
var bullet189 = series189.bullets.push(new am4charts.LabelBullet());
bullet189.interactionsEnabled = false;
bullet189.label.text = "1";
bullet189.label.fill = am4core.color("#ffffff");
bullet189.locationY = 0.5;
var series289 = chart89.series.push(new am4charts.ColumnSeries());
series289.columns.template.width = am4core.percent(80);
series289.columns.template.tooltipText =
"{name}: {valueY.totalPercent.formatNumber('#.00')}%";
series289.name = "2";
series289.dataFields.categoryX = "category89";
series289.dataFields.valueY = "value289";
series289.dataFields.valueYShow = "totalPercent";
series289.dataItems.template.locations.categoryX = 0.5;
series289.stacked = true;
series289.tooltip.pointerOrientation = "vertical";
var bullet289 = series289.bullets.push(new am4charts.LabelBullet());
bullet289.interactionsEnabled = false;
bullet289.label.text = "2";
bullet289.label.fill = am4core.color("#ffffff");
bullet289.locationY = 0.5;
var series389 = chart89.series.push(new am4charts.ColumnSeries());
series389.columns.template.width = am4core.percent(80);
series389.columns.template.tooltipText =
"{name}: {valueY.totalPercent.formatNumber('#.00')}%";
series389.name = "3";
series389.dataFields.categoryX = "category89";
series389.dataFields.valueY = "value389";
series389.dataFields.valueYShow = "totalPercent";
series389.dataItems.template.locations.categoryX = 0.5;
series389.stacked = true;
series389.tooltip.pointerOrientation = "vertical";
var bullet389 = series389.bullets.push(new am4charts.LabelBullet());
bullet389.interactionsEnabled = false;
bullet389.label.text = "3";
bullet389.locationY = 0.5;
bullet389.label.fill = am4core.color("#ffffff");
var series489 = chart89.series.push(new am4charts.ColumnSeries());
series489.columns.template.width = am4core.percent(80);
series489.columns.template.tooltipText =
"{name}: {valueY.totalPercent.formatNumber('#.00')}%";
series489.name = "4";
series489.dataFields.categoryX = "category89";
series489.dataFields.valueY = "value489";
series489.dataFields.valueYShow = "totalPercent";
series489.dataItems.template.locations.categoryX = 0.5;
series489.stacked = true;
series489.tooltip.pointerOrientation = "vertical";
var bullet489 = series489.bullets.push(new am4charts.LabelBullet());
bullet489.interactionsEnabled = false;
bullet489.label.text = "4";
bullet489.label.fill = am4core.color("#ffffff");
bullet489.locationY = 0.5;
var series589 = chart89.series.push(new am4charts.ColumnSeries());
series589.columns.template.width = am4core.percent(80);
series589.columns.template.tooltipText =
"{name}: {valueY.totalPercent.formatNumber('#.00')}%";
series589.name = "5";
series589.dataFields.categoryX = "category89";
series589.dataFields.valueY = "value589";
series589.dataFields.valueYShow = "totalPercent";
series589.dataItems.template.locations.categoryX = 0.5;
series589.stacked = true;
series589.tooltip.pointerOrientation = "vertical";
var bullet589 = series589.bullets.push(new am4charts.LabelBullet());
bullet589.interactionsEnabled = false;
bullet589.label.text = "5";
Also, an important note: I tried using only one at a time on the webpage and this still gave no different result. So probably not a duplicated variable I guess.
Problem solved. It was due to the particular design I copied/paste from somewhere else and gave to the divi textbox containing the charts.
I am trying to draw and fill a triangle. I referred an android - question How to draw filled triangle on android Canvas and did the following. It draws the triangle but it doesn’t fill it. How to get it filled? Also, is it possible to get it filled with a different color than the line color?
Xamarin Forms
private void OnPainting(object sender, SKPaintSurfaceEventArgs e)
{
var surface = e.Surface;
var canvas = surface.Canvas;
canvas.Clear(SKColors.White);
var pathStroke2 = new SKPaint
{
IsAntialias = true,
Style = SKPaintStyle.StrokeAndFill,
Color = new SKColor(244, 0, 110, 200),
StrokeWidth = 5
};
var path2 = new SKPath { FillType = SKPathFillType.EvenOdd };
path2.MoveTo(0, 0);
path2.LineTo(0, 140);
path2.MoveTo(0, 140);
path2.LineTo(140, 140);
path2.MoveTo(140, 140);
path2.LineTo(0, 0);
path2.Close();
canvas.DrawPath(path2, pathStroke2);
}
you don't need to use both LineTo() and MoveTo() - I suspect doing so is breaking the fill algorithm. Instead, just use
path2.MoveTo(0, 0);
path2.LineTo(0, 140);
path2.LineTo(140, 140);
path2.LineTo(0, 0);
path2.Close();
Removing unwanted MoveTo lines solved it, as per the comment from Jason.
Following is complete code.
private void OnPainting(object sender, SKPaintSurfaceEventArgs e)
{
var surface = e.Surface;
var canvas = surface.Canvas;
canvas.Clear(SKColors.White);
var w = e.Info.Width;
var h = e.Info.Height;
SKColor[] colors;
colors = new SKColor[] {
new SKColor(244, 0, 110, 200), new SKColor(244, 0, 110, 220),
new SKColor(244, 0, 110, 240),new SKColor(244, 0, 110, 240),
new SKColor(244, 0, 100,240),
new SKColor(244, 0, 100),
new SKColor(255, 0, 70)
};
SKShader shaderLeft = SKShader.CreateLinearGradient(new SKPoint(0, 0), new SKPoint(w, h), colors, null, SKShaderTileMode.Clamp);
var paint = new SKPaint() { Shader = shaderLeft };
canvas.DrawPaint(paint);
var path2 = new SKPath { FillType = SKPathFillType.EvenOdd };
path2.MoveTo(0, 0);
path2.LineTo(0, 140);
path2.LineTo(140, 140);
path2.LineTo(0, 0);
path2.Close();
var pathStroke2 = new SKPaint
{
IsAntialias = true,
Style = SKPaintStyle.StrokeAndFill,
Color = new SKColor(0, 0, 0, 30),
StrokeWidth = 5
};
using (var cf = SKColorFilter.CreateBlendMode(new SKColor(244, 0, 110, 200), SKBlendMode.DstIn))
{
var transparency = SKColors.White.WithAlpha(127);
pathStroke2.ColorFilter = cf;
// ... draw ...
canvas.DrawPath(path2, pathStroke2);
pathStroke2.ColorFilter = null;
}
var pathStroke3 = new SKPaint
{
IsAntialias = true,
Style = SKPaintStyle.StrokeAndFill,
Color = new SKColor(0, 0, 0, 30),
StrokeWidth = 5
};
var path3 = new SKPath { FillType = SKPathFillType.EvenOdd };
path3.MoveTo(170, 0);
path3.LineTo(170, 140);
path3.LineTo(300, 140);
path3.LineTo(170, 0);
path3.Close();
canvas.DrawPath(path3, pathStroke3);
}
I'm using DimpleJS to render a BubblePlot. My data looks like this:
[
{type: "A", name:"First", x:1, y:200},
{type: "A", name:"Second", x:30, y:10},
{type: "B", name:"Third", x:50, y:120},
{type: "B", name:"Fifth", x:90, y:100}
]
The graph is created with:
var myChart = new dimple.chart(svg, chartData);
myChart.setBounds(50, 30, 370, 230);
var x = myChart.addMeasureAxis("x", "x");
var y = myChart.addMeasureAxis("y", "y");
var series = myChart.addSeries(["type", "name"], dimple.plot.bubble);
myChart.addLegend(10, 10, 360, 20, "right");
myChart.draw();
This nearly does what I want, with all the data available in the tooltips etc. But coloring is based on both typeand name.
Also unfortunately the legend also picks up all the values from the name field where I'd prefer to just see the type values within the legend.
I also tried to the use the addColorAxismethod like this:
var c = myChart.addColorAxis("type");
var series = myChart.addSeries("name", dimple.plot.bubble);
But that renders black bubbles, shows "NaN" as type in the tooltips and putting that into a legend also doesn't seem to be possible.
Any suggestions are welcome!
Turns out that the order of arguments in the series is important.
This solved my problem:
var myChart = new dimple.chart(svg, chartData);
myChart.setBounds(50, 30, 370, 230);
var x = myChart.addMeasureAxis("x", "x");
var y = myChart.addMeasureAxis("y", "y");
var series = myChart.addSeries(["name","type"], dimple.plot.bubble);
myChart.addLegend(10, 10, 360, 20, "right");
myChart.draw();
I'm creating graphics by the code and placing them into a Bitmap. But they are higher than this Bitmap container.
import flash.display.*;
function getLine(){
var containerWidh:Number =enter code here 300;
var containerHeight:Number = 300;
var borderWidt:Number = 1;
var spriteWrap:Sprite = new Sprite();
var innerContainer:Sprite = new Sprite();
innerContainer.x = 0;
innerContainer.y = 0;
var line1:Shape = new Shape();
line1.graphics.lineStyle(5, 0x6F4356, 1, false, StageScaleMode.SHOW_ALL, CapsStyle.ROUND);
line1.graphics.moveTo(50, 5);
line1.graphics.lineTo(50, 800);
line1.graphics.endFill();
var line2:Shape = new Shape();
line2.graphics.lineStyle(5, 0x6F4356, 1, false, StageScaleMode.SHOW_ALL, CapsStyle.ROUND);
line2.graphics.moveTo(200, 290);
line2.graphics.lineTo(200, 300);
line2.graphics.endFill();
innerContainer.addChild(line1);
innerContainer.addChild(line2);
spriteWrap.addChild(innerContainer);
return spriteWrap;
}
var spriteWrap:Sprite = getLine();
var wrapForBitmap:Sprite = new Sprite();
var drawBitmap:BitmapData = new BitmapData(300, 300, true, 0x00ffaa);
var goOnStage:Bitmap = new Bitmap(drawBitmap);
wrapForBitmap.graphics.beginBitmapFill(drawBitmap);
wrapForBitmap.graphics.lineStyle(1, 0x6F7E84);
wrapForBitmap.graphics.drawRect(0, 0, 300, 300);
wrapForBitmap.graphics.endFill();
wrapForBitmap.x = 10;
wrapForBitmap.y = 10;
drawBitmap.draw(spriteWrap, new Matrix(1, 0, 0, 1, 0, 0));
wrapForBitmap.addChild(goOnStage);
stage.addChild(wrapForBitmap);
Yes of course. Than I redraw my Bitmap using my Sprite
MySprite.graphics.clear();
MySprite.graphics.beginBitmapFill(MyBitmap, new Matrix(1, 0, 0, 1, new_XPos, new_YPos), false, false);
MySprite.graphics.endFill();
Your Shape line1 is higher than your Bitmap's height (300):
line1.graphics.moveTo(50, 5); // begins at x = 50 and y = 5
line1.graphics.lineTo(50, 800); // begins at x = 50 and y = 800
With the following code, you will draw a line from the top to the bottom of your Bitmap:
line1.graphics.moveTo(50, 0);
line1.graphics.lineTo(50, 300);
If you want line1 to be visible without changing it's height, you have to change your Bitmap's height (800) by modifying the second argument of your BitmapData method:
var drawBitmap:BitmapData = new BitmapData(300, 800, true, 0x00ffaa);
You should modify at the same time your rectangle's height:
wrapForBitmap.graphics.drawRect(0, 0, 300, 800);
<canvas id="textureCanvas" width="3" height="1">what kind of cheap ass browser are you using? (I'm joking... something went wrong)</canvas>
window.onload = function () {
var mainCanvas = document.getElementById('mainCanvas');
var textureCanvas = document.getElementById('textureCanvas');
var mainContext = mainCanvas.getContext('2d');
var textureContext = textureCanvas.getContext('2d');
textureContext.fillStyle = 'grey';
textureContext.fillRect(0, 0, 1, 1);
textureContext.fillStyle = 'lightgrey';
textureContext.fillRect(1, 0, 1, 1);
textureContext.fillStyle = 'grey';
textureContext.fillRect(2, 0, 1, 1);
var pattern = mainCanvas.createPattern(textureCanvas, 'repeat');
mainCanvas.fillStyle = pattern;
mainCanvas.fillRect(0, 0, 198, 99);
};
I'm trying to use the first canvas as a pattern in the second canvas. It's an example from the Dummies guide. It's just a blank page that appears. I tried setting the background for the mainCanvas as green and it does show up. I guess I am a dummy.
The problem is that you are referring to mainCanvas and not mainContext when drawing on the mainCanvas. (I'm assuming you have a canvas element with and id of mainCanvas)
Change your code to:
var mainCanvas = document.getElementById('mainCanvas');
var textureCanvas = document.getElementById('textureCanvas');
var mainContext = mainCanvas.getContext('2d');
var textureContext = textureCanvas.getContext('2d');
textureContext.fillStyle = 'grey';
textureContext.fillRect(0, 0, 1, 1);
textureContext.fillStyle = 'lightgrey';
textureContext.fillRect(1, 0, 1, 1);
textureContext.fillStyle = 'grey';
textureContext.fillRect(2, 0, 1, 1);
var pattern = mainContext.createPattern(textureCanvas, 'repeat'); // <====
mainContext.fillStyle = pattern; // <====
mainContext.fillRect(0, 0, 198, 99); // <====
Example